001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.explorer;
019:
020: import java.awt.Dialog;
021: import java.awt.event.ActionEvent;
022: import java.util.logging.Logger;
023:
024: import javax.swing.AbstractAction;
025: import javax.swing.Action;
026: import javax.swing.JButton;
027: import javax.swing.JDialog;
028: import javax.swing.JLabel;
029: import javax.swing.JPanel;
030: import javax.swing.JTextField;
031: import javax.swing.JTree;
032: import javax.swing.tree.DefaultMutableTreeNode;
033: import javax.swing.tree.DefaultTreeModel;
034:
035: import com.jgoodies.forms.extras.SimpleFormBuilder;
036:
037: import de.finix.contelligent.client.base.ComponentFactory;
038: import de.finix.contelligent.client.base.ComponentNotFoundException;
039: import de.finix.contelligent.client.i18n.Resources;
040: import de.finix.contelligent.client.util.bookmark.Bookmark;
041: import de.finix.contelligent.client.util.bookmark.BookmarkFolder;
042: import de.finix.contelligent.client.util.bookmark.BookmarkManager;
043:
044: public class BookmarkPropertyDialog extends JDialog {
045:
046: static Logger log = Logger.getLogger(BookmarkPropertyDialog.class
047: .getName());
048:
049: private Bookmark bookmark = null;
050:
051: private JTree bookmarksTree = null;
052:
053: private JPanel panel = null;
054:
055: private JLabel nameLabel = null;
056:
057: private JTextField nameTextField = null;
058:
059: private JLabel componentPathLabel = null;
060:
061: private JTextField componentPathTextField = null;
062:
063: private JPanel buttonPanel = null;
064:
065: private JButton confirmButton = null;
066:
067: private JButton closeButton = null;
068:
069: /**
070: * This is the default constructor
071: */
072: public BookmarkPropertyDialog(Dialog owner, Bookmark bookmark,
073: JTree bookmarksTree) {
074: super (owner);
075:
076: this .bookmark = bookmark;
077: this .bookmarksTree = bookmarksTree;
078:
079: initialize();
080: }
081:
082: /**
083: * This method initializes this
084: *
085: * @return void
086: */
087: private void initialize() {
088: panel = new JPanel();
089:
090: // init layoutManager
091: SimpleFormBuilder builder = new SimpleFormBuilder(
092: "related,labelColumn=pref,related,fieldColumn=pref:grow,related,related",
093: "related,nameRow=pref,related,componentPathRow=pref,related,buttonRow=pref,related");
094: // Spalten und Zeilen
095:
096: builder.add(getNameLabel(), "labelColumn,nameRow");
097: builder.add(getNameTextField(), "fieldColumn,nameRow,2,1");
098:
099: builder.add(getComponentPathLabel(),
100: "labelColumn,componentPathRow");
101: builder.add(getComponentPathTextField(),
102: "fieldColumn,componentPathRow,2,1");
103:
104: builder.add(getButtonPanel(), "fieldColumn,buttonRow,2,1");
105:
106: panel = builder.getPanel();
107: panel.setOpaque(false);
108:
109: setContentPane(panel);
110: setTitle(Resources.getLocalStringForPackage(
111: "bookmark_property_editor_frame_title",
112: "languages.language"));
113:
114: getRootPane().setDefaultButton(confirmButton);
115:
116: setLocationByPlatform(true);
117: setSize(500, 115);
118: setResizable(false);
119:
120: setModal(true);
121: }
122:
123: /**
124: * @return the nameLabel
125: */
126: public JLabel getNameLabel() {
127: if (nameLabel == null) {
128: nameLabel = new JLabel();
129: nameLabel.setText(Resources.getLocalStringForPackage(
130: "bookmark_property_editor_name_label",
131: "languages.language")
132: + ": ");
133: }
134: return nameLabel;
135: }
136:
137: /**
138: * @return the nameTextField
139: */
140: private JTextField getNameTextField() {
141: if (nameTextField == null) {
142: nameTextField = new JTextField();
143: nameTextField.setText(this .bookmark.getName());
144: nameTextField
145: .setToolTipText(Resources
146: .getLocalStringForPackage(
147: "bookmark_property_editor_name_text_field_tool_tip",
148: "languages.language"));
149: nameTextField.setEditable(true);
150: nameTextField.setEnabled(true);
151: }
152: return nameTextField;
153: }
154:
155: /**
156: * @return the componentPathLabel
157: */
158: public JLabel getComponentPathLabel() {
159: if (componentPathLabel == null) {
160: componentPathLabel = new JLabel();
161: componentPathLabel
162: .setText(Resources
163: .getLocalStringForPackage(
164: "bookmark_property_editor_componentpath_label",
165: "languages.language")
166: + ": ");
167: }
168: return componentPathLabel;
169: }
170:
171: /**
172: * @return the componentPathTextField
173: */
174: public JTextField getComponentPathTextField() {
175: if (componentPathTextField == null) {
176: componentPathTextField = new JTextField();
177: componentPathTextField.setText(this .bookmark
178: .getComponentPath());
179:
180: componentPathTextField.setEditable(true);
181: componentPathTextField.setEnabled(true);
182: }
183: return componentPathTextField;
184: }
185:
186: /**
187: * @return the buttonPanel
188: */
189: public JPanel getButtonPanel() {
190: if (buttonPanel == null) {
191: buttonPanel = new JPanel();
192:
193: // init layoutManager
194: SimpleFormBuilder builder = new SimpleFormBuilder(
195: "related,confirmButtonColumn=pref,related,closeButtonColumn=pref,related",
196: "row=pref");
197: // Spalten und Zeilen
198:
199: builder.add(getConfirmButton(), "confirmButtonColumn,row");
200: builder.add(getCloseButton(), "closeButtonColumn,row");
201:
202: buttonPanel = builder.getPanel();
203: buttonPanel.setOpaque(false);
204: }
205: return buttonPanel;
206: }
207:
208: /**
209: * @return the confirmButton
210: */
211: public JButton getConfirmButton() {
212: if (confirmButton == null) {
213: confirmButton = new JButton(confirmAction);
214:
215: confirmButton.setText(Resources.getLocalStringForPackage(
216: "bookmark_property_editor_confirm_button",
217: "languages.language"));
218: }
219: return confirmButton;
220: }
221:
222: /**
223: * @return the closeButton
224: */
225: public JButton getCloseButton() {
226: if (closeButton == null) {
227: closeButton = new JButton(closeFrameAction);
228:
229: closeButton.setText(Resources.getLocalStringForPackage(
230: "bookmark_property_editor_close_button",
231: "languages.language"));
232: }
233: return closeButton;
234: }
235:
236: /**
237: *
238: */
239: Action confirmAction = new AbstractAction("confirmButton") {
240: // This method is called when the button is pressed
241: public void actionPerformed(ActionEvent evt) {
242:
243: // create new bookmarkFolder
244: String name = getNameTextField().getText().trim();
245: String componentPath = getComponentPathTextField()
246: .getText();
247:
248: // bookmark.getType() liefert null!? darum wird hier typ neu ermittelt:
249: String type = null;
250:
251: try {
252: type = ComponentFactory.getInstance().getComponent(
253: componentPath).getTypeName();
254: } catch (ComponentNotFoundException e) {
255: log
256: .warning("Could not get component '"
257: + componentPath
258: + "' to determine its component type and icon.");
259:
260: // bacause the componentPath might be invalid, determining the type might fail. so we set it here.
261: type = "";
262: }
263:
264: Bookmark bookmarkNew = new Bookmark(componentPath, name,
265: type);
266:
267: // replace bookmark = remove old and add new bookmark at the same node
268: BookmarkManager bookmarkManager = BookmarkManager
269: .getInstance();
270: bookmarkManager.replace(bookmarkNew, bookmark);
271:
272: // update tree visualization
273: // Because we know, that the model is of type DefaultTreeModel, we can explicit cast on it.
274: DefaultTreeModel treeModel = (DefaultTreeModel) bookmarksTree
275: .getModel();
276: DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) bookmarksTree
277: .getLastSelectedPathComponent();
278:
279: // get parent of selected node
280: DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selectedNode
281: .getParent();
282:
283: if (parent == null) {
284: // if parent is null, something went wrong.
285: // we close this dialog to enforce a revisualization of the tree.
286: dispose();
287: }
288:
289: DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(
290: name);
291: newNode.setUserObject(bookmarkNew);
292:
293: // remove old node
294: treeModel.removeNodeFromParent(selectedNode);
295:
296: int childCount = parent.getChildCount();
297: if (childCount > 0) {
298: String compareName = null;
299:
300: for (int i = 0; i < childCount; i++) {
301: DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent
302: .getChildAt(i);
303:
304: if (node.getUserObject() instanceof Bookmark) {
305: Bookmark bookmark = (Bookmark) node
306: .getUserObject();
307: compareName = bookmark.getName();
308:
309: if (bookmarkNew.getName()
310: .compareTo(compareName) > 0) {
311: // Bookmark name is bigger than compareName
312: if ((i + 1) == childCount) {
313: // add new node at the end of list
314: treeModel.insertNodeInto(newNode,
315: parent, i + 1);
316: break;
317: }
318: } else {
319: // add new node
320: treeModel
321: .insertNodeInto(newNode, parent, i);
322: break;
323: }
324: } else {
325: if (node.getUserObject() instanceof BookmarkFolder) {
326: if ((i + 1) == childCount) {
327: // add new node at the end of list
328: treeModel.insertNodeInto(newNode,
329: parent, i + 1);
330: break;
331: }
332: }
333: }
334: }
335: } else {
336: // no children, then insert at index 0.
337: treeModel.insertNodeInto(newNode, parent, 0);
338: treeModel.reload();
339: }
340:
341: dispose();
342: }
343: };
344:
345: /**
346: *
347: */
348: Action closeFrameAction = new AbstractAction("closeButton") {
349: // This method is called when the button is pressed
350: public void actionPerformed(ActionEvent evt) {
351:
352: dispose();
353: }
354: };
355: }
|