rename template->variant in the code

This commit is contained in:
Branislav Stojkovic
2019-01-07 23:51:22 +01:00
parent a4639d3f1a
commit fc33be65ab
8 changed files with 72 additions and 70 deletions
@@ -51,7 +51,7 @@ import org.diylc.core.Theme;
* @see IMessageListener * @see IMessageListener
* @see EventType * @see EventType
*/ */
public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, ITemplateProcessor, public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, IVariantProcessor,
IBlockProcessor { IBlockProcessor {
public static final String ANTI_ALIASING_KEY = "antiAliasing"; public static final String ANTI_ALIASING_KEY = "antiAliasing";
@@ -25,26 +25,26 @@ import java.util.List;
import org.diylc.core.Template; import org.diylc.core.Template;
public interface ITemplateProcessor { public interface IVariantProcessor {
public static final String TEMPLATES_KEY = "templates"; public static final String TEMPLATES_KEY = "templates";
public static final String DEFAULT_TEMPLATES_KEY = "defaultTemplates"; public static final String DEFAULT_TEMPLATES_KEY = "defaultTemplates";
void saveSelectedComponentAsTemplate(String templateName); void saveSelectedComponentAsVariant(String variantName);
List<Template> getTemplatesFor(String categoryName, String componentTypeName); List<Template> getVariantsFor(String categoryName, String componentTypeName);
List<Template> getTemplatesForSelection(); List<Template> getVariantsForSelection();
void applyTemplateToSelection(Template template); void applyVariantToSelection(Template variant);
void deleteTemplate(String categoryName, String componentTypeName, String templateName); void deleteVariant(String categoryName, String componentTypeName, String variantName);
void setTemplateDefault(String categoryName, String componentTypeName, String templateName); void setDefaultVariant(String categoryName, String componentTypeName, String variantName);
String getDefaultTemplate(String categoryName, String componentTypeName); String getDefaultVariant(String categoryName, String componentTypeName);
public class TemplateAlreadyExistsException extends Exception { public class VariantAlreadyExistsException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -2091,8 +2091,8 @@ public class Presenter implements IPlugInPort {
// try to find a default template if none is provided // try to find a default template if none is provided
if (componentType != null && template == null) { if (componentType != null && template == null) {
String defaultTemplate = getDefaultTemplate(componentType.getCategory(), componentType.getName()); String defaultTemplate = getDefaultVariant(componentType.getCategory(), componentType.getName());
List<Template> templates = getTemplatesFor(componentType.getCategory(), componentType.getName()); List<Template> templates = getVariantsFor(componentType.getCategory(), componentType.getName());
if (templates != null && defaultTemplate != null) if (templates != null && defaultTemplate != null)
for (Template t : templates) { for (Template t : templates) {
if (t.getName().equals(defaultTemplate)) { if (t.getName().equals(defaultTemplate)) {
@@ -2122,25 +2122,25 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void saveSelectedComponentAsTemplate(String templateName) { public void saveSelectedComponentAsVariant(String variantName) {
LOG.info(String.format("saveSelectedComponentAsTemplate(%s)", templateName)); LOG.info(String.format("saveSelectedComponentAsVariant(%s)", variantName));
if (selectedComponents.size() != 1) { if (selectedComponents.size() != 1) {
throw new RuntimeException("Can only save a single component as a template at once."); throw new RuntimeException("Can only save a single component as a variant at once.");
} }
IDIYComponent<?> component = selectedComponents.iterator().next(); IDIYComponent<?> component = selectedComponents.iterator().next();
ComponentType type = ComponentType type =
ComponentProcessor.getInstance().extractComponentTypeFrom( ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) component.getClass()); (Class<? extends IDIYComponent<?>>) component.getClass());
Map<String, List<Template>> templateMap = Map<String, List<Template>> variantMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null); (Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
if (templateMap == null) { if (variantMap == null) {
templateMap = new HashMap<String, List<Template>>(); variantMap = new HashMap<String, List<Template>>();
} }
String key = type.getCategory() + "." + type.getName(); String key = type.getCategory() + "." + type.getName();
List<Template> templates = templateMap.get(key); List<Template> variants = variantMap.get(key);
if (templates == null) { if (variants == null) {
templates = new ArrayList<Template>(); variants = new ArrayList<Template>();
templateMap.put(key, templates); variantMap.put(key, variants);
} }
List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties(component.getClass()); List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties(component.getClass());
Map<String, Object> values = new HashMap<String, Object>(); Map<String, Object> values = new HashMap<String, Object>();
@@ -2166,10 +2166,10 @@ public class Presenter implements IPlugInPort {
point.translate(-x, -y); point.translate(-x, -y);
} }
Template template = new Template(templateName, values, points); Template template = new Template(variantName, values, points);
boolean exists = false; boolean exists = false;
for (Template t : templates) { for (Template t : variants) {
if (t.getName().equalsIgnoreCase(templateName)) { if (t.getName().equalsIgnoreCase(variantName)) {
exists = true; exists = true;
break; break;
} }
@@ -2177,29 +2177,29 @@ public class Presenter implements IPlugInPort {
if (exists) { if (exists) {
int result = int result =
view.showConfirmDialog("Template with that name already exists. Overwrite?", "Save as Template", view.showConfirmDialog("A variant with that name already exists. Overwrite?", "Save as Variant",
IView.YES_NO_OPTION, IView.WARNING_MESSAGE); IView.YES_NO_OPTION, IView.WARNING_MESSAGE);
if (result != IView.YES_OPTION) { if (result != IView.YES_OPTION) {
return; return;
} }
// Delete the existing template // Delete the existing variant
Iterator<Template> i = templates.iterator(); Iterator<Template> i = variants.iterator();
while (i.hasNext()) { while (i.hasNext()) {
Template t = i.next(); Template t = i.next();
if (t.getName().equalsIgnoreCase(templateName)) { if (t.getName().equalsIgnoreCase(variantName)) {
i.remove(); i.remove();
} }
} }
} }
templates.add(template); variants.add(template);
if (System.getProperty("org.diylc.WriteStaticVariants", "false").equalsIgnoreCase("true")) if (System.getProperty("org.diylc.WriteStaticVariants", "false").equalsIgnoreCase("true"))
{ {
if (defaultVariantMap == null) if (defaultVariantMap == null)
defaultVariantMap = new HashMap<String, List<Template>>(); defaultVariantMap = new HashMap<String, List<Template>>();
// unify default and user-variants // unify default and user-variants
for(Map.Entry<String, List<Template>> entry : templateMap.entrySet()) { for(Map.Entry<String, List<Template>> entry : variantMap.entrySet()) {
if (defaultVariantMap.containsKey(entry.getKey())) { if (defaultVariantMap.containsKey(entry.getKey())) {
defaultVariantMap.get(entry.getKey()).addAll(entry.getValue()); defaultVariantMap.get(entry.getKey()).addAll(entry.getValue());
} else { } else {
@@ -2218,13 +2218,13 @@ public class Presenter implements IPlugInPort {
LOG.error("Could not save default variants", e); LOG.error("Could not save default variants", e);
} }
} else { } else {
ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, templateMap); ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, variantMap);
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public List<Template> getTemplatesFor(String categoryName, String componentTypeName) { public List<Template> getVariantsFor(String categoryName, String componentTypeName) {
Map<String, List<Template>> variantMap = Map<String, List<Template>> variantMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null); (Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
List<Template> variants = new ArrayList<Template>(); List<Template> variants = new ArrayList<Template>();
@@ -2243,7 +2243,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public List<Template> getTemplatesForSelection() { public List<Template> getVariantsForSelection() {
if (this.selectedComponents.isEmpty()) { if (this.selectedComponents.isEmpty()) {
throw new RuntimeException("No components selected"); throw new RuntimeException("No components selected");
} }
@@ -2256,13 +2256,13 @@ public class Presenter implements IPlugInPort {
if (selectedType == null) if (selectedType == null)
selectedType = type; selectedType = type;
else if (selectedType.getInstanceClass() != type.getInstanceClass()) else if (selectedType.getInstanceClass() != type.getInstanceClass())
throw new RuntimeException("Template can be applied on multiple components of the same type only"); return null;
} }
return getTemplatesFor(selectedType.getCategory(), selectedType.getName()); return getVariantsFor(selectedType.getCategory(), selectedType.getName());
} }
@Override @Override
public void applyTemplateToSelection(Template template) { public void applyVariantToSelection(Template template) {
LOG.debug(String.format("applyTemplateToSelection(%s)", template.getName())); LOG.debug(String.format("applyTemplateToSelection(%s)", template.getName()));
Project oldProject = currentProject.clone(); Project oldProject = currentProject.clone();
@@ -2289,7 +2289,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void deleteTemplate(String categoryName, String componentTypeName, String templateName) { public void deleteVariant(String categoryName, String componentTypeName, String templateName) {
LOG.debug(String.format("deleteTemplate(%s, %s, %s)", categoryName, componentTypeName, templateName)); LOG.debug(String.format("deleteTemplate(%s, %s, %s)", categoryName, componentTypeName, templateName));
Map<String, List<Template>> templateMap = Map<String, List<Template>> templateMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null); (Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
@@ -2310,7 +2310,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public void setTemplateDefault(String categoryName, String componentTypeName, String templateName) { public void setDefaultVariant(String categoryName, String componentTypeName, String templateName) {
LOG.debug(String.format("setTemplateDefault(%s, %s, %s)", categoryName, componentTypeName, templateName)); LOG.debug(String.format("setTemplateDefault(%s, %s, %s)", categoryName, componentTypeName, templateName));
Map<String, String> defaultTemplateMap = Map<String, String> defaultTemplateMap =
(Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null); (Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
@@ -2325,7 +2325,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Override @Override
public String getDefaultTemplate(String categoryName, String componentTypeName) { public String getDefaultVariant(String categoryName, String componentTypeName) {
Map<String, String> defaultTemplateMap = Map<String, String> defaultTemplateMap =
(Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null); (Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
if (defaultTemplateMap == null) if (defaultTemplateMap == null)
@@ -1011,7 +1011,7 @@ public class ActionFactory {
String templateName = String templateName =
JOptionPane.showInputDialog(null, "Variant name:", "Save as Variant", JOptionPane.PLAIN_MESSAGE); JOptionPane.showInputDialog(null, "Variant name:", "Save as Variant", JOptionPane.PLAIN_MESSAGE);
if (templateName != null && !templateName.trim().isEmpty()) { if (templateName != null && !templateName.trim().isEmpty()) {
plugInPort.saveSelectedComponentAsTemplate(templateName); plugInPort.saveSelectedComponentAsVariant(templateName);
} }
} }
} }
@@ -413,14 +413,16 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
List<Template> templates = null; List<Template> templates = null;
try { try {
templates = plugInPort.getTemplatesForSelection(); templates = plugInPort.getVariantsForSelection();
} catch (Exception e) { } catch (Exception e) {
LOG.info("Could not load variants for selection"); LOG.info("Could not load variants for selection");
getApplyTemplateMenu().setEnabled(false); getApplyTemplateMenu().setEnabled(false);
} }
if (templates == null) if (templates == null) {
getApplyTemplateMenu().setEnabled(false);
return; return;
}
getApplyTemplateMenu().setEnabled(templates.size() > 0); getApplyTemplateMenu().setEnabled(templates.size() > 0);
@@ -431,7 +433,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
plugInPort.applyTemplateToSelection(finalTemplate); plugInPort.applyVariantToSelection(finalTemplate);
} }
}); });
getApplyTemplateMenu().add(item); getApplyTemplateMenu().add(item);
@@ -106,9 +106,9 @@ public class ComponentButtonFactory {
return button; return button;
} }
public static JMenuItem createTemplateItem(final IPlugInPort plugInPort, final Template template, public static JMenuItem createVariantItem(final IPlugInPort plugInPort, final Template variant,
final ComponentType componentType) { final ComponentType componentType) {
final JMenuItem item = new JMenuItem(template.getName()) { final JMenuItem item = new JMenuItem(variant.getName()) {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@@ -122,14 +122,14 @@ public class ComponentButtonFactory {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
plugInPort.setNewComponentTypeSlot(componentType, template); plugInPort.setNewComponentTypeSlot(componentType, variant);
} }
}); });
String defaultVariant = plugInPort.getDefaultTemplate(componentType.getCategory(), componentType.getName()); String defaultVariant = plugInPort.getDefaultVariant(componentType.getCategory(), componentType.getName());
JLabel label = new JLabel(template.getName().equals(defaultVariant) ? IconLoader.PinGreen.getIcon() : IconLoader.PinGrey.getIcon()); JLabel label = new JLabel(variant.getName().equals(defaultVariant) ? IconLoader.PinGreen.getIcon() : IconLoader.PinGrey.getIcon());
label.setToolTipText(template.getName().equals(defaultVariant) ? "Remove default variant" : "Set default variant"); label.setToolTipText(variant.getName().equals(defaultVariant) ? "Remove default variant" : "Set default variant");
label.addMouseListener(new MouseAdapter() { label.addMouseListener(new MouseAdapter() {
@Override @Override
@@ -140,7 +140,7 @@ public class ComponentButtonFactory {
JPopupMenu m = (JPopupMenu) c; JPopupMenu m = (JPopupMenu) c;
m.setVisible(false); m.setVisible(false);
} }
plugInPort.setTemplateDefault(componentType.getCategory(), componentType.getName(), template.getName()); plugInPort.setDefaultVariant(componentType.getCategory(), componentType.getName(), variant.getName());
e.consume(); e.consume();
} }
}); });
@@ -163,11 +163,11 @@ public class ComponentButtonFactory {
} }
int result = int result =
JOptionPane.showConfirmDialog(SwingUtilities.getRoot(item), "Are you sure you want to delete variant \"" JOptionPane.showConfirmDialog(SwingUtilities.getRoot(item), "Are you sure you want to delete variant \""
+ template.getName() + "\"", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE); + variant.getName() + "\"", "Delete", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (result != JOptionPane.YES_OPTION) { if (result != JOptionPane.YES_OPTION) {
return; return;
} }
plugInPort.deleteTemplate(componentType.getCategory(), componentType.getName(), template.getName()); plugInPort.deleteVariant(componentType.getCategory(), componentType.getName(), variant.getName());
e.consume(); e.consume();
} }
}); });
@@ -160,7 +160,7 @@ class ComponentTabbedPane extends JTabbedPane {
toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS)); toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
for (ComponentType componentType : componentTypes) { for (ComponentType componentType : componentTypes) {
try { try {
Component button = ComponentButtonFactory.create(plugInPort, componentType, createTemplatePopup(componentType)); Component button = ComponentButtonFactory.create(plugInPort, componentType, createVariantPopup(componentType));
toolbar.add(button); toolbar.add(button);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Could not create recent component button for " + componentType.getName(), e); LOG.error("Could not create recent component button for " + componentType.getName(), e);
@@ -204,7 +204,7 @@ class ComponentTabbedPane extends JTabbedPane {
componentType = componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom( ComponentProcessor.getInstance().extractComponentTypeFrom(
(Class<? extends IDIYComponent<?>>) Class.forName(componentClassName)); (Class<? extends IDIYComponent<?>>) Class.forName(componentClassName));
Component button = ComponentButtonFactory.create(plugInPort, componentType, createTemplatePopup(componentType)); Component button = ComponentButtonFactory.create(plugInPort, componentType, createVariantPopup(componentType));
toolbar.add(button); toolbar.add(button);
} catch (Exception e) { } catch (Exception e) {
LOG.error("Could not create recent component button for " + componentClassName, e); LOG.error("Could not create recent component button for " + componentClassName, e);
@@ -212,23 +212,23 @@ class ComponentTabbedPane extends JTabbedPane {
} }
} }
private JPopupMenu createTemplatePopup(final ComponentType componentType) { private JPopupMenu createVariantPopup(final ComponentType componentType) {
final JPopupMenu templatePopup = new JPopupMenu(); final JPopupMenu variantPopup = new JPopupMenu();
templatePopup.add("Loading..."); variantPopup.add("Loading...");
templatePopup.addPopupMenuListener(new PopupMenuListener() { variantPopup.addPopupMenuListener(new PopupMenuListener() {
@Override @Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) { public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
templatePopup.removeAll(); variantPopup.removeAll();
List<Template> templates = plugInPort.getTemplatesFor(componentType.getCategory(), componentType.getName()); List<Template> variants = plugInPort.getVariantsFor(componentType.getCategory(), componentType.getName());
if (templates == null || templates.isEmpty()) { if (variants == null || variants.isEmpty()) {
JMenuItem item = new JMenuItem("<no variants>"); JMenuItem item = new JMenuItem("<no variants>");
item.setEnabled(false); item.setEnabled(false);
templatePopup.add(item); variantPopup.add(item);
} else { } else {
for (Template template : templates) { for (Template variant : variants) {
JMenuItem item = ComponentButtonFactory.createTemplateItem(plugInPort, template, componentType); JMenuItem item = ComponentButtonFactory.createVariantItem(plugInPort, variant, componentType);
templatePopup.add(item); variantPopup.add(item);
} }
} }
} }
@@ -239,6 +239,6 @@ class ComponentTabbedPane extends JTabbedPane {
@Override @Override
public void popupMenuCanceled(PopupMenuEvent e) {} public void popupMenuCanceled(PopupMenuEvent e) {}
}); });
return templatePopup; return variantPopup;
} }
} }
@@ -441,14 +441,14 @@ public class TreePanel extends JPanel {
popup.add(shortcutSubmenu); popup.add(shortcutSubmenu);
popup.add(new JSeparator()); popup.add(new JSeparator());
List<Template> templates = plugInPort.getTemplatesFor(componentType.getCategory(), componentType.getName()); List<Template> templates = plugInPort.getVariantsFor(componentType.getCategory(), componentType.getName());
if (templates == null || templates.isEmpty()) { if (templates == null || templates.isEmpty()) {
JMenuItem item = new JMenuItem("<no variants>"); JMenuItem item = new JMenuItem("<no variants>");
item.setEnabled(false); item.setEnabled(false);
popup.add(item); popup.add(item);
} else { } else {
for (Template template : templates) { for (Template template : templates) {
JMenuItem item = ComponentButtonFactory.createTemplateItem(plugInPort, template, componentType); JMenuItem item = ComponentButtonFactory.createVariantItem(plugInPort, template, componentType);
popup.add(item); popup.add(item);
} }
} }
@@ -628,7 +628,7 @@ public class TreePanel extends JPanel {
} }
if (payload.getComponentType() != null) { if (payload.getComponentType() != null) {
List<Template> variants = plugInPort.getTemplatesFor(payload.getComponentType().getCategory(), payload.getComponentType().getName()); List<Template> variants = plugInPort.getVariantsFor(payload.getComponentType().getCategory(), payload.getComponentType().getName());
if (variants != null && !variants.isEmpty()) { if (variants != null && !variants.isEmpty()) {
variantsHtml = " <a style=\"text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; background-color: #D7FFC6; color: #666666;\">[+" variantsHtml = " <a style=\"text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black; background-color: #D7FFC6; color: #666666;\">[+"
+ variants.size() + "]</a>"; + variants.size() + "]</a>";