added default templates

This commit is contained in:
bancika
2017-01-05 12:36:04 +01:00
parent 53f09e6143
commit 980055d4c0
10 changed files with 85 additions and 7 deletions
@@ -18,6 +18,8 @@ public interface ITemplateProcessor {
void deleteTemplate(String categoryName, String componentTypeName, String templateName);
void setTemplateDefault(String categoryName, String componentTypeName, String templateName, boolean defaultFlag);
public class TemplateAlreadyExistsException extends Exception {
private static final long serialVersionUID = 1L;
@@ -9,12 +9,14 @@ public class Template {
private String name;
private Map<String, Object> values;
private List<Point> points;
private boolean defaultFlag;
public Template(String name, Map<String, Object> values, List<Point> points) {
public Template(String name, Map<String, Object> values, List<Point> points, boolean defaultFlag) {
super();
this.name = name;
this.values = values;
this.points = points;
this.defaultFlag = defaultFlag;
}
public String getName() {
@@ -41,10 +43,19 @@ public class Template {
this.points = points;
}
public boolean isDefaultFlag() {
return defaultFlag;
}
public void setDefaultFlag(boolean defaultFlag) {
this.defaultFlag = defaultFlag;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (defaultFlag ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((points == null) ? 0 : points.hashCode());
result = prime * result + ((values == null) ? 0 : values.hashCode());
@@ -60,6 +71,8 @@ public class Template {
if (getClass() != obj.getClass())
return false;
Template other = (Template) obj;
if (defaultFlag != other.defaultFlag)
return false;
if (name == null) {
if (other.name != null)
return false;
@@ -77,4 +90,9 @@ public class Template {
return false;
return true;
}
@Override
public String toString() {
return name;
}
}
@@ -1790,6 +1790,18 @@ public class Presenter implements IPlugInPort {
setNewComponentTypeSlot(null, null);
return;
}
// try to find a default template if none is provided
if (componentType != null && template == null) {
List<Template> templates = getTemplatesFor(componentType.getCategory(), componentType.getName());
for (Template t : templates) {
if (t.isDefaultFlag()) {
template = t;
break;
}
}
}
try {
instantiationManager.setComponentTypeSlot(componentType, template, currentProject);
if (componentType != null) {
@@ -1854,7 +1866,7 @@ public class Presenter implements IPlugInPort {
point.translate(-x, -y);
}
Template template = new Template(templateName, values, points);
Template template = new Template(templateName, values, points, false);
boolean exists = false;
for (Template t : templates) {
if (t.getName().equalsIgnoreCase(templateName)) {
@@ -1958,6 +1970,24 @@ public class Presenter implements IPlugInPort {
}
}
}
ConfigurationManager.getInstance().saveConfigration();
}
@SuppressWarnings("unchecked")
@Override
public void setTemplateDefault(String categoryName, String componentTypeName, String templateName, boolean defaultFlag) {
Map<String, List<Template>> templateMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
if (templateMap != null) {
List<Template> templates = templateMap.get(categoryName + "." + componentTypeName);
if (templates != null) {
for (Template template : templates)
template.setDefaultFlag(defaultFlag && templateName.equals(template.getName()));
ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, templateMap);
return;
}
}
throw new RuntimeException("Could not find the specified template.");
}
private Set<IDIYComponent<?>> getLockedComponents() {
Binary file not shown.
Binary file not shown.
@@ -36,8 +36,9 @@ public enum IconLoader {
"cloud_edit.png"), CloudUpload("cloud_upload.png"), NavLeftBlue("nav_left_blue.png"), NavRightBlue(
"nav_right_blue.png"), MissingImage("missing_image.png"), CloudBg("cloud_bg.png"), CloudBig("cloud_big.png"), CloudWait(
"cloud_wait.png"), Spinning("spinning.gif"), Megaphone("megaphone.png"), Download("download.png"), Eye("eye.png"), Messages(
"messages.png"), SearchBox("search-box.png"), Screwdriver("screwdriver.png"), Hammer("hammer.png"), FlipHorizontal("flip_horizontal.png"),
FlipVertical("flip_vertical.png"), MagicWand("magic_wand.png");
"messages.png"), SearchBox("search-box.png"), Screwdriver("screwdriver.png"), Hammer("hammer.png"), FlipHorizontal(
"flip_horizontal.png"), FlipVertical("flip_vertical.png"), MagicWand("magic_wand.png"), PinGrey("pin_grey.png"), PinGreen(
"pin_green.png");
protected String name;
Binary file not shown.

After

Width:  |  Height:  |  Size: 722 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 708 B

@@ -97,7 +97,7 @@ public class ComponentButtonFactory {
// Customize item size to fit the delete button
public java.awt.Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
return new Dimension(d.width + 18, d.height);
return new Dimension(d.width + 32, d.height);
}
};
item.addActionListener(new ActionListener() {
@@ -107,7 +107,30 @@ public class ComponentButtonFactory {
plugInPort.setNewComponentTypeSlot(componentType, template);
}
});
JLabel label = new JLabel(IconLoader.Garbage.getIcon());
JLabel label = new JLabel(template.isDefaultFlag() ? IconLoader.PinGreen.getIcon() : IconLoader.PinGrey.getIcon());
label.setToolTipText(template.isDefaultFlag() ? "Remove default template" : "Set default template");
label.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Hide the menu
Container c = item.getParent();
if (c != null && c instanceof JPopupMenu) {
JPopupMenu m = (JPopupMenu) c;
m.setVisible(false);
}
plugInPort.setTemplateDefault(componentType.getCategory(), componentType.getName(), template.getName(),
!template.isDefaultFlag());
e.consume();
}
});
Border margin = new EmptyBorder(4, 0, 0, 0);
label.setBorder(margin);
item.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
item.add(label);
label = new JLabel(IconLoader.Garbage.getIcon());
label.setToolTipText("Delete template");
label.addMouseListener(new MouseAdapter() {
@@ -129,7 +152,7 @@ public class ComponentButtonFactory {
e.consume();
}
});
Border margin = new EmptyBorder(4, 0, 0, 0);
margin = new EmptyBorder(4, 2, 0, 0);
label.setBorder(margin);
item.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
item.add(label);
+4
View File
@@ -1522,6 +1522,10 @@
<changeType>NEW_FEATURE</changeType>
<description>Mirror selection horizontally and vertically</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>NEW_FEATURE</changeType>
<description>Ability to set a default template for component type (grey "pin" icon in the template popup)</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Default focus on "Text" field for label</description>