001: /*--------------------------------------------------------------------------*
002: | Copyright (C) 2006 Christopher Kohlhaas |
003: | |
004: | This program is free software; you can redistribute it and/or modify |
005: | it under the terms of the GNU General Public License as published by the |
006: | Free Software Foundation. A copy of the license has been included with |
007: | these distribution in the COPYING file, if not go to www.fsf.org |
008: | |
009: | As a special exception, you are granted the permissions to link this |
010: | program with every library, which license fulfills the Open Source |
011: | Definition as published by the Open Source Initiative (OSI). |
012: *--------------------------------------------------------------------------*/
013: package org.rapla.gui.internal.edit;
014:
015: import java.awt.BorderLayout;
016: import java.awt.Color;
017: import java.awt.Component;
018: import java.awt.Dimension;
019: import java.awt.Graphics;
020: import java.awt.Insets;
021: import java.util.ArrayList;
022: import java.util.Collections;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Locale;
026: import java.util.Map;
027:
028: import javax.swing.BorderFactory;
029: import javax.swing.JComponent;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JSplitPane;
033: import javax.swing.border.Border;
034: import javax.swing.border.TitledBorder;
035: import javax.swing.event.ChangeEvent;
036: import javax.swing.event.ChangeListener;
037: import javax.swing.tree.DefaultMutableTreeNode;
038: import javax.swing.tree.DefaultTreeModel;
039:
040: import org.rapla.entities.Named;
041: import org.rapla.entities.NamedComparator;
042: import org.rapla.entities.configuration.Preferences;
043: import org.rapla.framework.PluginDescriptor;
044: import org.rapla.framework.RaplaContext;
045: import org.rapla.framework.RaplaException;
046: import org.rapla.gui.DefaultPluginOption;
047: import org.rapla.gui.EditComponent;
048: import org.rapla.gui.OptionPanel;
049: import org.rapla.gui.RaplaGUIComponent;
050: import org.rapla.gui.TreeFactory;
051: import org.rapla.gui.toolkit.RaplaTree;
052: import org.rapla.gui.toolkit.RaplaWidget;
053: import org.rapla.plugin.RaplaExtensionPoints;
054:
055: public class PreferencesEditUI extends RaplaGUIComponent implements
056: RaplaWidget, EditComponent, ChangeListener {
057: private JSplitPane content = new JSplitPane(
058: JSplitPane.HORIZONTAL_SPLIT);
059: protected TitledBorder selectionBorder;
060: protected RaplaTree jPanelSelection = new RaplaTree();
061: protected JPanel jPanelContainer = new JPanel();
062: protected JPanel container = new JPanel();
063: JLabel messages = new JLabel();
064: JPanel defaultPanel = new JPanel();
065: OptionPanel lastOptionPanel;
066: Preferences preferences;
067:
068: /** called during initialization to create the info component */
069: public PreferencesEditUI(RaplaContext sm) throws RaplaException {
070: super (sm);
071: jPanelContainer.setLayout(new BorderLayout());
072: jPanelContainer.add(messages, BorderLayout.NORTH);
073: messages.setForeground(Color.red);
074: Border emptyLineBorder = new Border() {
075: Insets insets = new Insets(2, 0, 2, 0);
076: Color COLOR = Color.LIGHT_GRAY;
077:
078: public void paintBorder(Component c, Graphics g, int x,
079: int y, int width, int height) {
080: g.setColor(COLOR);
081: g.drawLine(0, 1, c.getWidth(), 1);
082: g.drawLine(0, c.getHeight() - 2, c.getWidth(), c
083: .getHeight() - 2);
084: }
085:
086: public Insets getBorderInsets(Component c) {
087: return insets;
088: }
089:
090: public boolean isBorderOpaque() {
091: return true;
092: }
093: };
094: content.setBorder(emptyLineBorder);
095: jPanelContainer.add(content, BorderLayout.CENTER);
096: jPanelSelection.getTree().setCellRenderer(
097: getTreeFactory().createRenderer());
098: jPanelSelection.setToolTipRenderer(getTreeFactory()
099: .createTreeToolTipRenderer());
100: container.setPreferredSize(new Dimension(650, 400));
101: content.setLeftComponent(jPanelSelection);
102: content.setRightComponent(container);
103: content.setDividerLocation(200);
104: Border emptyBorder = BorderFactory
105: .createEmptyBorder(4, 4, 4, 4);
106: selectionBorder = BorderFactory.createTitledBorder(emptyBorder,
107: getString("selection") + ":");
108: jPanelSelection.setBorder(selectionBorder);
109: content.setResizeWeight(0.4);
110: jPanelSelection.addChangeListener(this );
111: }
112:
113: final private TreeFactory getTreeFactory() {
114: return (TreeFactory) getService(TreeFactory.ROLE);
115: }
116:
117: protected OptionPanel[] getPluginOptions() throws RaplaException {
118: Map optionMap = getContainer().lookupServicesFor(
119: RaplaExtensionPoints.PLUGIN_OPTION_PANEL_EXTENSION);
120: List optionList = new ArrayList();
121: List pluginList = (List) getService(PluginDescriptor.PLUGIN_LIST);
122: for (Iterator it = pluginList.iterator(); it.hasNext();) {
123: final PluginDescriptor plugin = (PluginDescriptor) it
124: .next();
125: OptionPanel optionPanel = (OptionPanel) optionMap
126: .get(plugin.getClass().getName());
127: if (optionPanel == null) {
128: optionPanel = new DefaultPluginOption(getContext()) {
129:
130: public String getDescriptorClassName() {
131: return plugin.getClass().getName();
132: }
133:
134: public String getName(Locale locale) {
135: return plugin.toString();
136: }
137:
138: };
139: }
140: optionList.add(optionPanel);
141: }
142: sort(optionList);
143: return (OptionPanel[]) optionList.toArray(new OptionPanel[] {});
144: }
145:
146: public void sort(List list) {
147: Collections.sort(list, new NamedComparator(getRaplaLocale()
148: .getLocale()));
149: }
150:
151: public OptionPanel[] getUserOptions() throws RaplaException {
152: Map optionMap = getContainer().lookupServicesFor(
153: RaplaExtensionPoints.USER_OPTION_PANEL_EXTENSION);
154: List optionList = new ArrayList(optionMap.values());
155: sort(optionList);
156: return (OptionPanel[]) optionList.toArray(new OptionPanel[] {});
157: }
158:
159: public OptionPanel[] getAdminOptions() throws RaplaException {
160: Map optionMap = getContainer().lookupServicesFor(
161: RaplaExtensionPoints.SYSTEM_OPTION_PANEL_EXTENSION);
162: List optionList = new ArrayList(optionMap.values());
163: sort(optionList);
164: return (OptionPanel[]) optionList.toArray(new OptionPanel[] {});
165: }
166:
167: protected JComponent createInfoComponent() {
168: JPanel panel = new JPanel();
169: return panel;
170: }
171:
172: private void setOptionPanel(OptionPanel optionPanel)
173: throws Exception {
174: String title = getString("nothing_selected");
175: JComponent comp = defaultPanel;
176: if (optionPanel != null) {
177: title = optionPanel.getName(getRaplaLocale().getLocale());
178: comp = optionPanel.getComponent();
179: }
180:
181: TitledBorder titledBorder = new TitledBorder(BorderFactory
182: .createEmptyBorder(4, 4, 4, 4), title);
183: container.removeAll();
184: container.setLayout(new BorderLayout());
185: container.setBorder(titledBorder);
186: container.add(comp, BorderLayout.CENTER);
187: container.revalidate();
188: container.repaint();
189: }
190:
191: public String getTitle() {
192: return getString("options");
193: }
194:
195: /** maps all fields back to the current object.*/
196: public void mapToObject() throws RaplaException {
197: if (lastOptionPanel != null)
198: lastOptionPanel.commit();
199: }
200:
201: public void setObject(Object o) throws RaplaException {
202: this .preferences = (Preferences) o;
203: if (preferences.getOwner() == null) {
204: messages
205: .setText("You need to restart Rapla/Rapla-server when you change the options!");
206: }
207: TreeFactory f = getTreeFactory();
208: DefaultMutableTreeNode root = new DefaultMutableTreeNode("");
209: if (preferences.getOwner() != null) {
210: Named[] element = getUserOptions();
211: for (int i = 0; i < element.length; i++) {
212: root.add(f.newNamedNode(element[i]));
213: }
214: } else {
215: {
216: Named[] element = getAdminOptions();
217: DefaultMutableTreeNode adminRoot = new DefaultMutableTreeNode(
218: "admin-options");
219: for (int i = 0; i < element.length; i++) {
220: adminRoot.add(f.newNamedNode(element[i]));
221: }
222: root.add(adminRoot);
223: }
224: {
225: Named[] element = getPluginOptions();
226: DefaultMutableTreeNode pluginRoot = new DefaultMutableTreeNode(
227: "plugins");
228: for (int i = 0; i < element.length; i++) {
229: pluginRoot.add(f.newNamedNode(element[i]));
230: }
231: root.add(pluginRoot);
232: }
233: }
234: DefaultTreeModel treeModel = new DefaultTreeModel(root);
235: jPanelSelection.exchangeTreeModel(treeModel);
236: }
237:
238: public Object getObject() {
239: return preferences;
240: }
241:
242: public void stateChanged(ChangeEvent evt) {
243: try {
244: if (lastOptionPanel != null)
245: lastOptionPanel.commit();
246:
247: OptionPanel optionPanel = null;
248: if (getSelectedElement() instanceof OptionPanel) {
249: optionPanel = (OptionPanel) getSelectedElement();
250: if (optionPanel != null) {
251: optionPanel.setPreferences(preferences);
252: optionPanel.show();
253: }
254: }
255: lastOptionPanel = optionPanel;
256: setOptionPanel(lastOptionPanel);
257: } catch (Exception ex) {
258: showException(ex, getComponent());
259: }
260: }
261:
262: public Object getSelectedElement() {
263: return jPanelSelection.getSelectedElement();
264: }
265:
266: public JComponent getComponent() {
267: return jPanelContainer;
268: }
269:
270: }
|