private void addRule() { // We must add it just after the currently selected Rule. // Let's find which one it is. If there is none, we add it at the // end of the list. MultiInputPanel mip = new MultiInputPanel(I18N.tr("Choose a name for your rule")); mip.addInput("RuleName", I18N.tr("Name of the Rule : "), new TextBoxType(10)); mip.addValidation( new MIPValidation() { @Override public String validate(MultiInputPanel mid) { String ruleName = mid.getInput("RuleName"); return ruleName.isEmpty() ? I18N.tr("Rule name cannot be null or empty.") : null; } }); if (UIFactory.showDialog(mip)) { String s = mip.getInput("RuleName"); LegendTreeModel tm = (LegendTreeModel) tree.getModel(); // We need to link our new RuleWrapper with the layer we are editing. Rule temp = new Rule(simpleStyleEditor.getStyleWrapper().getStyle().getLayer()); temp.setName(s); Legend leg = LegendFactory.getLegend(temp.getCompositeSymbolizer().getSymbolizerList().get(0)); // Initialize a panel for this legend. ILegendPanel ilp = ILegendPanelFactory.getILegendPanel(simpleStyleEditor, leg); List<ILegendPanel> list = new ArrayList<ILegendPanel>(); list.add(ilp); RuleWrapper nrw = new RuleWrapper(simpleStyleEditor, temp, list); tm.addElement(tm.getRoot(), nrw, getSelectedRule()); simpleStyleEditor.legendAdded(nrw.getPanel()); } }
/** * Selects the first legend attached to the given style in this {@link LegendTree} and displays it * in the Simple Style Editor's card layout. * * @param style Style */ private void selectAndShowFirstLegend(StyleWrapper style) { RuleWrapper firstRW = style.getRuleWrapper(0); ILegendPanel firstPanel = firstRW.getLegend(0); TreePath tp = new TreePath(style).pathByAddingChild(firstRW).pathByAddingChild(firstPanel); tree.setSelectionPath(tp); simpleStyleEditor.showDialogForLegend(firstPanel); }
/** * Removes the currently selected element from the tree. If it is an inner node, all its children * will be lost. */ public void removeSelectedElement() { TreePath tp = tree.getSelectionPath(); Object select = tp.getLastPathComponent(); LegendTreeModel tm = (LegendTreeModel) tree.getModel(); if (select instanceof ILegendPanel) { RuleWrapper rw = (RuleWrapper) tp.getPath()[tp.getPath().length - 2]; tree.setSelectionPath(null); tm.removeElement(rw, select); // We refresh the legend container simpleStyleEditor.showDialogForCurrentlySelectedLegend(); // We refresh the icons } else if (select instanceof RuleWrapper) { tree.setSelectionPath(null); tm.removeElement(tm.getRoot(), select); simpleStyleEditor.showDialogForCurrentlySelectedLegend(); } refreshIcons(); }
/** * Add a legend to the tree, in the currently selected RuleWrapper, after the currently selected * Legend (if any in both case). A RuleWrapper will be added in the case there is none. */ private void addLegend() { LegendUIChooser legendPicker = new LegendUIChooser(simpleStyleEditor); if (UIFactory.showDialog(legendPicker)) { // Recover the panel that was selected when the user clicked OK. ILegendPanel ilp = legendPicker.getSelectedPanel(); // Get the currently selected RuleWrapper, or the last one in this // style if none is currently selected. RuleWrapper currentrw = getSelectedRule(); StyleWrapper sw = simpleStyleEditor.getStyleWrapper(); if (currentrw == null) { if (sw.getSize() == 0) { addRule(); } currentrw = sw.getRuleWrapper(sw.getSize() - 1); } // Set the Legend's name. Legend legend = ilp.getLegend(); legend .getSymbolizer() .setName(getUniqueName(legend.getLegendTypeName(), currentrw.getRule(), 0)); // Add the panel to the LegendTree. ((LegendTreeModel) tree.getModel()).addElement(currentrw, ilp, getSelectedLegend()); // Automatically select the newly added legend in the tree. TreePath selectionPath = tree.getSelectionPath(); TreePath parent; if (selectionPath.getLastPathComponent() instanceof RuleWrapper) { parent = selectionPath; } else { parent = selectionPath.getParentPath(); } tree.setSelectionPath(parent.pathByAddingChild(ilp)); // Notify the SimpleStyleEditor that a Legend has been added. simpleStyleEditor.legendAdded(ilp); } }
/** Refreshes the state of the icons contained in the toolbar of this panel. */ public final void refreshIcons() { // We must retrieve the index of the currently selected item in its // parent to decide if we display the buttons or not. TreePath tp = tree.getSelectionPath(); if (tp == null) { jButtonMenuDel.setEnabled(false); jButtonMenuRename.setEnabled(false); jButtonMenuDown.setEnabled(false); jButtonMenuUp.setEnabled(false); } else { jButtonMenuRename.setEnabled(true); Object last = tp.getLastPathComponent(); int index = -1; int max = -1; if (last instanceof StyleWrapper) { max = 0; index = 0; } else if (last instanceof RuleWrapper) { StyleWrapper sw = simpleStyleEditor.getStyleWrapper(); index = sw.indexOf((RuleWrapper) last); max = sw.getSize() - 1; } else if (last instanceof ILegendPanel) { RuleWrapper rw = getSelectedRule(); index = rw.indexOf((ILegendPanel) last); max = rw.getSize() - 1; } if (index == 0) { jButtonMenuUp.setEnabled(false); } else { jButtonMenuUp.setEnabled(true); } if (index < max) { jButtonMenuDown.setEnabled(true); } else { jButtonMenuDown.setEnabled(false); } if (max < 1) { jButtonMenuDel.setEnabled(false); } else { jButtonMenuDel.setEnabled(true); } } }
public LegendTree(final SimpleStyleEditor simpleEditor) { simpleStyleEditor = simpleEditor; StyleWrapper style = simpleStyleEditor.getStyleWrapper(); // We create our tree tree = new JTree(); // We don't want to display the root. tree.setRootVisible(true); // We have a custom model to provide... Listeners on the TreeModel // are added by the tree when calling setModel. LegendTreeModel ltm = new LegendTreeModel(tree, style); tree.setModel(ltm); // ..A custom cell editor... LegendTreeCellEditor editor = new LegendTreeCellEditor(); editor.setClickCountToStart(2); tree.setCellEditor(editor); // ...and a custom TreeCellRenderer. LegendCellRenderer lcr = new LegendCellRenderer(tree); tree.setCellRenderer(lcr); // We want to select only one element at a time. tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); selectAndShowFirstLegend(style); // We refresh icons when the selection changes. TreeSelectionListener tsl = EventHandler.create(TreeSelectionListener.class, this, "refreshIcons"); tree.addTreeSelectionListener(tsl); // We refresh the CardLayout of the associated SimpleStyleEditor TreeSelectionListener select = EventHandler.create(TreeSelectionListener.class, simpleStyleEditor, "legendSelected"); tree.addTreeSelectionListener(select); expandAll(tree); // We want an editable tree tree.setEditable(true); initButtons(); this.setLayout(new BorderLayout()); this.add(toolBar, BorderLayout.PAGE_START); JScrollPane scrollPane = new JScrollPane(tree); this.add(scrollPane, BorderLayout.CENTER); refreshIcons(); }