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 EventType
*/
public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, ITemplateProcessor,
public interface IPlugInPort extends ISelectionProcessor, IMouseProcessor, IKeyProcessor, IVariantProcessor,
IBlockProcessor {
public static final String ANTI_ALIASING_KEY = "antiAliasing";
@@ -25,26 +25,26 @@ import java.util.List;
import org.diylc.core.Template;
public interface ITemplateProcessor {
public interface IVariantProcessor {
public static final String TEMPLATES_KEY = "templates";
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;
@@ -2091,8 +2091,8 @@ public class Presenter implements IPlugInPort {
// try to find a default template if none is provided
if (componentType != null && template == null) {
String defaultTemplate = getDefaultTemplate(componentType.getCategory(), componentType.getName());
List<Template> templates = getTemplatesFor(componentType.getCategory(), componentType.getName());
String defaultTemplate = getDefaultVariant(componentType.getCategory(), componentType.getName());
List<Template> templates = getVariantsFor(componentType.getCategory(), componentType.getName());
if (templates != null && defaultTemplate != null)
for (Template t : templates) {
if (t.getName().equals(defaultTemplate)) {
@@ -2122,25 +2122,25 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@Override
public void saveSelectedComponentAsTemplate(String templateName) {
LOG.info(String.format("saveSelectedComponentAsTemplate(%s)", templateName));
public void saveSelectedComponentAsVariant(String variantName) {
LOG.info(String.format("saveSelectedComponentAsVariant(%s)", variantName));
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();
ComponentType type =
ComponentProcessor.getInstance().extractComponentTypeFrom(
(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);
if (templateMap == null) {
templateMap = new HashMap<String, List<Template>>();
if (variantMap == null) {
variantMap = new HashMap<String, List<Template>>();
}
String key = type.getCategory() + "." + type.getName();
List<Template> templates = templateMap.get(key);
if (templates == null) {
templates = new ArrayList<Template>();
templateMap.put(key, templates);
List<Template> variants = variantMap.get(key);
if (variants == null) {
variants = new ArrayList<Template>();
variantMap.put(key, variants);
}
List<PropertyWrapper> properties = ComponentProcessor.getInstance().extractProperties(component.getClass());
Map<String, Object> values = new HashMap<String, Object>();
@@ -2166,10 +2166,10 @@ public class Presenter implements IPlugInPort {
point.translate(-x, -y);
}
Template template = new Template(templateName, values, points);
Template template = new Template(variantName, values, points);
boolean exists = false;
for (Template t : templates) {
if (t.getName().equalsIgnoreCase(templateName)) {
for (Template t : variants) {
if (t.getName().equalsIgnoreCase(variantName)) {
exists = true;
break;
}
@@ -2177,29 +2177,29 @@ public class Presenter implements IPlugInPort {
if (exists) {
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);
if (result != IView.YES_OPTION) {
return;
}
// Delete the existing template
Iterator<Template> i = templates.iterator();
// Delete the existing variant
Iterator<Template> i = variants.iterator();
while (i.hasNext()) {
Template t = i.next();
if (t.getName().equalsIgnoreCase(templateName)) {
if (t.getName().equalsIgnoreCase(variantName)) {
i.remove();
}
}
}
templates.add(template);
variants.add(template);
if (System.getProperty("org.diylc.WriteStaticVariants", "false").equalsIgnoreCase("true"))
{
if (defaultVariantMap == null)
defaultVariantMap = new HashMap<String, List<Template>>();
// 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())) {
defaultVariantMap.get(entry.getKey()).addAll(entry.getValue());
} else {
@@ -2218,13 +2218,13 @@ public class Presenter implements IPlugInPort {
LOG.error("Could not save default variants", e);
}
} else {
ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, templateMap);
ConfigurationManager.getInstance().writeValue(TEMPLATES_KEY, variantMap);
}
}
@SuppressWarnings("unchecked")
@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>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
List<Template> variants = new ArrayList<Template>();
@@ -2243,7 +2243,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@Override
public List<Template> getTemplatesForSelection() {
public List<Template> getVariantsForSelection() {
if (this.selectedComponents.isEmpty()) {
throw new RuntimeException("No components selected");
}
@@ -2256,13 +2256,13 @@ public class Presenter implements IPlugInPort {
if (selectedType == null)
selectedType = type;
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
public void applyTemplateToSelection(Template template) {
public void applyVariantToSelection(Template template) {
LOG.debug(String.format("applyTemplateToSelection(%s)", template.getName()));
Project oldProject = currentProject.clone();
@@ -2289,7 +2289,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@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));
Map<String, List<Template>> templateMap =
(Map<String, List<Template>>) ConfigurationManager.getInstance().readObject(TEMPLATES_KEY, null);
@@ -2310,7 +2310,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@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));
Map<String, String> defaultTemplateMap =
(Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
@@ -2325,7 +2325,7 @@ public class Presenter implements IPlugInPort {
@SuppressWarnings("unchecked")
@Override
public String getDefaultTemplate(String categoryName, String componentTypeName) {
public String getDefaultVariant(String categoryName, String componentTypeName) {
Map<String, String> defaultTemplateMap =
(Map<String, String>) ConfigurationManager.getInstance().readObject(DEFAULT_TEMPLATES_KEY, null);
if (defaultTemplateMap == null)
@@ -1011,7 +1011,7 @@ public class ActionFactory {
String templateName =
JOptionPane.showInputDialog(null, "Variant name:", "Save as Variant", JOptionPane.PLAIN_MESSAGE);
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;
try {
templates = plugInPort.getTemplatesForSelection();
templates = plugInPort.getVariantsForSelection();
} catch (Exception e) {
LOG.info("Could not load variants for selection");
getApplyTemplateMenu().setEnabled(false);
}
if (templates == null)
if (templates == null) {
getApplyTemplateMenu().setEnabled(false);
return;
}
getApplyTemplateMenu().setEnabled(templates.size() > 0);
@@ -431,7 +433,7 @@ public class CanvasPlugin implements IPlugIn, ClipboardOwner {
@Override
public void actionPerformed(ActionEvent e) {
plugInPort.applyTemplateToSelection(finalTemplate);
plugInPort.applyVariantToSelection(finalTemplate);
}
});
getApplyTemplateMenu().add(item);
@@ -106,9 +106,9 @@ public class ComponentButtonFactory {
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 JMenuItem item = new JMenuItem(template.getName()) {
final JMenuItem item = new JMenuItem(variant.getName()) {
private static final long serialVersionUID = 1L;
@@ -122,14 +122,14 @@ public class ComponentButtonFactory {
@Override
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());
label.setToolTipText(template.getName().equals(defaultVariant) ? "Remove default variant" : "Set default variant");
JLabel label = new JLabel(variant.getName().equals(defaultVariant) ? IconLoader.PinGreen.getIcon() : IconLoader.PinGrey.getIcon());
label.setToolTipText(variant.getName().equals(defaultVariant) ? "Remove default variant" : "Set default variant");
label.addMouseListener(new MouseAdapter() {
@Override
@@ -140,7 +140,7 @@ public class ComponentButtonFactory {
JPopupMenu m = (JPopupMenu) c;
m.setVisible(false);
}
plugInPort.setTemplateDefault(componentType.getCategory(), componentType.getName(), template.getName());
plugInPort.setDefaultVariant(componentType.getCategory(), componentType.getName(), variant.getName());
e.consume();
}
});
@@ -163,11 +163,11 @@ public class ComponentButtonFactory {
}
int result =
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) {
return;
}
plugInPort.deleteTemplate(componentType.getCategory(), componentType.getName(), template.getName());
plugInPort.deleteVariant(componentType.getCategory(), componentType.getName(), variant.getName());
e.consume();
}
});
@@ -160,7 +160,7 @@ class ComponentTabbedPane extends JTabbedPane {
toolbar.setLayout(new BoxLayout(toolbar, BoxLayout.X_AXIS));
for (ComponentType componentType : componentTypes) {
try {
Component button = ComponentButtonFactory.create(plugInPort, componentType, createTemplatePopup(componentType));
Component button = ComponentButtonFactory.create(plugInPort, componentType, createVariantPopup(componentType));
toolbar.add(button);
} catch (Exception e) {
LOG.error("Could not create recent component button for " + componentType.getName(), e);
@@ -204,7 +204,7 @@ class ComponentTabbedPane extends JTabbedPane {
componentType =
ComponentProcessor.getInstance().extractComponentTypeFrom(
(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);
} catch (Exception 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) {
final JPopupMenu templatePopup = new JPopupMenu();
templatePopup.add("Loading...");
templatePopup.addPopupMenuListener(new PopupMenuListener() {
private JPopupMenu createVariantPopup(final ComponentType componentType) {
final JPopupMenu variantPopup = new JPopupMenu();
variantPopup.add("Loading...");
variantPopup.addPopupMenuListener(new PopupMenuListener() {
@Override
public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
templatePopup.removeAll();
List<Template> templates = plugInPort.getTemplatesFor(componentType.getCategory(), componentType.getName());
if (templates == null || templates.isEmpty()) {
variantPopup.removeAll();
List<Template> variants = plugInPort.getVariantsFor(componentType.getCategory(), componentType.getName());
if (variants == null || variants.isEmpty()) {
JMenuItem item = new JMenuItem("<no variants>");
item.setEnabled(false);
templatePopup.add(item);
variantPopup.add(item);
} else {
for (Template template : templates) {
JMenuItem item = ComponentButtonFactory.createTemplateItem(plugInPort, template, componentType);
templatePopup.add(item);
for (Template variant : variants) {
JMenuItem item = ComponentButtonFactory.createVariantItem(plugInPort, variant, componentType);
variantPopup.add(item);
}
}
}
@@ -239,6 +239,6 @@ class ComponentTabbedPane extends JTabbedPane {
@Override
public void popupMenuCanceled(PopupMenuEvent e) {}
});
return templatePopup;
return variantPopup;
}
}
@@ -441,14 +441,14 @@ public class TreePanel extends JPanel {
popup.add(shortcutSubmenu);
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()) {
JMenuItem item = new JMenuItem("<no variants>");
item.setEnabled(false);
popup.add(item);
} else {
for (Template template : templates) {
JMenuItem item = ComponentButtonFactory.createTemplateItem(plugInPort, template, componentType);
JMenuItem item = ComponentButtonFactory.createVariantItem(plugInPort, template, componentType);
popup.add(item);
}
}
@@ -628,7 +628,7 @@ public class TreePanel extends JPanel {
}
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()) {
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>";