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.composed;
019:
020: import java.awt.BorderLayout;
021: import java.awt.Toolkit;
022: import java.awt.datatransfer.Clipboard;
023: import java.awt.datatransfer.ClipboardOwner;
024: import java.awt.datatransfer.Transferable;
025: import java.awt.datatransfer.UnsupportedFlavorException;
026: import java.awt.event.ActionEvent;
027: import java.awt.event.FocusAdapter;
028: import java.awt.event.FocusEvent;
029: import java.io.IOException;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.logging.Level;
034: import java.util.logging.Logger;
035:
036: import javax.swing.AbstractAction;
037: import javax.swing.Action;
038: import javax.swing.JLabel;
039: import javax.swing.JOptionPane;
040: import javax.swing.JTabbedPane;
041: import javax.swing.event.ChangeEvent;
042: import javax.swing.event.ChangeListener;
043:
044: import de.finix.contelligent.client.base.ComponentFactory;
045: import de.finix.contelligent.client.base.ComponentNotFoundException;
046: import de.finix.contelligent.client.base.ContelligentComponent;
047: import de.finix.contelligent.client.base.Type;
048: import de.finix.contelligent.client.event.ContelligentEvent;
049: import de.finix.contelligent.client.gui.AbstractComponentEditor;
050: import de.finix.contelligent.client.gui.ComponentEditor;
051: import de.finix.contelligent.client.gui.ComponentRenderer;
052: import de.finix.contelligent.client.gui.ContelligentAction;
053: import de.finix.contelligent.client.gui.GUI;
054: import de.finix.contelligent.client.gui.GUIFactory;
055: import de.finix.contelligent.client.gui.UnsupportedGUIException;
056: import de.finix.contelligent.client.gui.text.StringOptionPane;
057: import de.finix.contelligent.client.i18n.Resources;
058: import de.finix.contelligent.client.remote.ActionResult;
059: import de.finix.contelligent.client.remote.Actions;
060: import de.finix.contelligent.client.remote.RemoteActionException;
061: import de.finix.contelligent.client.util.ComponentMovement;
062: import de.finix.contelligent.client.util.ExceptionDialog;
063: import de.finix.contelligent.client.util.dnd.SingleComponentTransferable;
064:
065: public class TabbedEditor extends AbstractComponentEditor implements
066: ClipboardOwner {
067:
068: private static Logger logger = Logger.getLogger(TabbedEditor.class
069: .getName());
070:
071: private JTabbedPane tabbedPane = null;
072:
073: private List componentGUI;
074:
075: private AddTabAction addTabAction = new AddTabAction();
076:
077: private DeleteTabAction deleteTabAction = new DeleteTabAction();
078:
079: private CopyTabAction copyTabAction = new CopyTabAction();
080:
081: private PasteTabAction pasteTabAction = new PasteTabAction();
082:
083: private MoveUpAction moveUpAction = new MoveUpAction();
084:
085: private MoveDownAction moveDownAction = new MoveDownAction();
086:
087: public void init() {
088: tabbedPane = new JTabbedPane();
089:
090: update();
091: add(tabbedPane, BorderLayout.CENTER);
092:
093: tabbedPane.addFocusListener(new FocusAdapter() {
094: public void focusGained(FocusEvent e) {
095: getView().setActions(getActions());
096: }
097: });
098:
099: tabbedPane.addChangeListener(new ChangeListener() {
100: public void stateChanged(ChangeEvent e) {
101: if (getComponent().getType().getName().equals(
102: Type.SORTED_FOLDER)) {
103:
104: if (tabbedPane.getSelectedIndex() == tabbedPane
105: .getTabCount() - 1) {
106: moveDownAction.setEnabled(false);
107: } else {
108: moveDownAction.setEnabled(true);
109: }
110:
111: if (tabbedPane.getSelectedIndex() == 0) {
112: moveUpAction.setEnabled(false);
113: } else {
114: moveUpAction.setEnabled(true);
115: }
116:
117: } else {
118: moveDownAction.setEnabled(false);
119: moveUpAction.setEnabled(false);
120: }
121: getView().setActions(getActions());
122: }
123: });
124: }
125:
126: public void update() {
127:
128: int selectedTab = tabbedPane.getSelectedIndex();
129: tabbedPane.removeAll();
130: componentGUI = new ArrayList();
131:
132: for (Iterator iterator = getComponent().getSubcomponents()
133: .iterator(); iterator.hasNext();) {
134: String subcomponentName = (String) iterator.next();
135: try {
136: ContelligentComponent subcomponent = ComponentFactory
137: .getInstance().getComponent(
138: getComponent().getPath() + "/"
139: + subcomponentName);
140: GUI[] gui = GUIFactory.getInstance().getGUI(
141: subcomponent, getView());
142: ComponentRenderer compGUI;
143: if (isEditable()) {
144: compGUI = gui[0].getEditor(GUI.DEFAULT);
145: } else {
146: compGUI = gui[0].getRenderer(GUI.DEFAULT);
147: }
148: tabbedPane.addTab(subcomponent.getName(),
149: (java.awt.Component) compGUI);
150: componentGUI.add(compGUI);
151: } catch (UnsupportedGUIException uge) {
152: tabbedPane.addTab(subcomponentName, new JLabel("[-]"));
153: } catch (ComponentNotFoundException cnfe) {
154: // this component is not valid anymore, so it will not be added
155: // to the tabs
156: logger.log(Level.WARNING, "Subcomponent "
157: + subcomponentName + " no longer valid", cnfe);
158: }
159: }
160:
161: tabbedPane.validate();
162: tabbedPane.repaint();
163:
164: if ((selectedTab > -1)
165: && (selectedTab < tabbedPane.getTabCount())) {
166: tabbedPane.setSelectedIndex(selectedTab);
167: }
168:
169: enableActions();
170: }
171:
172: private void enableActions() {
173: // if (
174: // getComponent().getPropertyValue(ContelligentComponent.BLUEPRINT_PATH).length()
175: // > 0 ) {
176: // addTabAction.setEnabled(true);
177: // } else {
178: addTabAction.setEnabled(false);
179: // }
180:
181: if (tabbedPane.getTabCount() > 0) {
182: deleteTabAction.setEnabled(true);
183: } else {
184: deleteTabAction.setEnabled(false);
185: }
186: }
187:
188: public void setEditable(boolean editable) {
189: super .setEditable(editable);
190: if (tabbedPane != null) {
191: update();
192: }
193: }
194:
195: public void rollback() {
196: for (int i = 0; i < componentGUI.size(); i++) {
197: ((ComponentEditor) componentGUI.get(i)).rollback();
198: }
199: super .rollback();
200: }
201:
202: public void commit() {
203: for (int i = 0; i < componentGUI.size(); i++) {
204: ((ComponentEditor) componentGUI.get(i)).commit();
205: }
206: super .commit();
207: }
208:
209: public Action[] getActions() {
210:
211: if (isEditable()) {
212: return new Action[] { moveUpAction, moveDownAction,
213: addTabAction, deleteTabAction, copyTabAction,
214: pasteTabAction };
215: } else {
216: return new Action[] {};
217: }
218: }
219:
220: // implements interface ClipboardOwner
221: public void lostOwnership(Clipboard clipboard, Transferable contents) {
222: logger.log(Level.FINE, "Ownership lost");
223: }
224:
225: protected void updateComponent() {
226: }
227:
228: protected void componentChanged(ContelligentEvent event) {
229: update();
230: }
231:
232: protected void childComponentAdded(ContelligentEvent event) {
233: update();
234: }
235:
236: protected void childComponentRemoved(ContelligentEvent event) {
237: update();
238: }
239:
240: protected void childComponentChanged(ContelligentEvent event) {
241: update();
242: }
243:
244: protected void descendentComponentChanged(ContelligentEvent event) {
245: }
246:
247: public class AddTabAction extends AbstractAction implements
248: ContelligentAction {
249:
250: public AddTabAction() {
251: super ("add_tab", Resources.newIcon);
252: putValue(ROLLOVER_ICON, Resources.newIconRollOver);
253: putValue(TYPE, PUSH_ACTION);
254: putValue(ACTION_TYPE, EDIT_ACTION);
255: putValue(MENU_TARGET, MENU);
256: putValue(BUTTON_TARGET, TOOLBAR);
257: }
258:
259: public void actionPerformed(ActionEvent e) {
260: try {
261: String blueprint = getComponent().getPropertyValue(
262: ContelligentComponent.BLUEPRINT_PATH);
263: String name = blueprint.substring(blueprint
264: .lastIndexOf('/') + 1);
265:
266: // ask for the name of the new component
267: StringOptionPane stringOptionPane = new StringOptionPane();
268: if (stringOptionPane.showStringDialog(Resources
269: .getLocalString("component_name"), Resources
270: .getLocalString("new_component_name")
271: + ":", name) == StringOptionPane.OK_OPTION) {
272: name = stringOptionPane.getText();
273: if (name.length() > 0) {
274: // FIXME doesnt work because blueprint is wrong!!!!
275: ActionResult result = Actions.create(getView()
276: .getEnvironment(), getComponent()
277: .getPath(), name, blueprint);
278: result.showErrors();
279: }
280: }
281: } catch (RemoteActionException rae) {
282: ExceptionDialog.show(rae);
283: }
284: }
285: }
286:
287: public class DeleteTabAction extends AbstractAction implements
288: ContelligentAction {
289:
290: public DeleteTabAction() {
291: super ("delete_tab", Resources.deleteIcon);
292: putValue(ROLLOVER_ICON, Resources.deleteIconRollOver);
293: putValue(TYPE, PUSH_ACTION);
294: putValue(ACTION_TYPE, EDIT_ACTION);
295: putValue(MENU_TARGET, MENU);
296: putValue(BUTTON_TARGET, TOOLBAR);
297: }
298:
299: public void actionPerformed(ActionEvent e) {
300: try {
301: int answer = JOptionPane
302: .showConfirmDialog(
303: null,
304: Resources
305: .getLocalString("delete_selected_tab_question"),
306: Resources
307: .getLocalString("comfirm_delete"),
308: JOptionPane.YES_NO_OPTION);
309: if (answer == JOptionPane.YES_OPTION) {
310: String name = TabbedEditor.this .tabbedPane
311: .getTitleAt(TabbedEditor.this .tabbedPane
312: .getSelectedIndex());
313: ActionResult result = Actions.delete(getView()
314: .getEnvironment(), getComponent().getPath()
315: + "/" + name);
316: result.showErrors();
317: }
318: } catch (RemoteActionException rae) {
319: ExceptionDialog.show(rae);
320: }
321: }
322: }
323:
324: public class CopyTabAction extends AbstractAction implements
325: ContelligentAction {
326:
327: public CopyTabAction() {
328: super ("copy_tab", Resources.copyIcon);
329: putValue(ROLLOVER_ICON, Resources.copyIconRollOver);
330: putValue(TYPE, PUSH_ACTION);
331: putValue(ACTION_TYPE, EDIT_ACTION);
332: putValue(MENU_TARGET, MENU);
333: putValue(BUTTON_TARGET, TOOLBAR);
334: }
335:
336: public void actionPerformed(ActionEvent e) {
337: try {
338: String path = getComponent().getPath()
339: + "/"
340: + tabbedPane.getTitleAt(tabbedPane
341: .getSelectedIndex());
342: // copy component to clipboard
343: ContelligentComponent component = ComponentFactory
344: .getInstance().getComponent(path);
345: Clipboard clipboard = Toolkit.getDefaultToolkit()
346: .getSystemClipboard();
347: clipboard.setContents(new SingleComponentTransferable(
348: component, getView().getEnvironment()),
349: (ClipboardOwner) this );
350: } catch (ComponentNotFoundException cnfe) {
351: ExceptionDialog.show(cnfe);
352: }
353: }
354: }
355:
356: public class PasteTabAction extends AbstractAction implements
357: ContelligentAction {
358: public PasteTabAction() {
359: super ("paste_tab", Resources.pasteIcon);
360: putValue(ROLLOVER_ICON, Resources.pasteIconRollOver);
361: putValue(TYPE, PUSH_ACTION);
362: putValue(ACTION_TYPE, EDIT_ACTION);
363: putValue(MENU_TARGET, MENU);
364: putValue(BUTTON_TARGET, TOOLBAR);
365: }
366:
367: public void actionPerformed(ActionEvent e) {
368: Clipboard clipboard = Toolkit.getDefaultToolkit()
369: .getSystemClipboard();
370: Transferable clipData = clipboard.getContents(clipboard);
371:
372: if (clipData != null) {
373: // ask for the name of the new component
374: StringOptionPane stringOptionPane = new StringOptionPane();
375: if (stringOptionPane.showStringDialog(Resources
376: .getLocalString("component_name"), Resources
377: .getLocalString("new_component_name")
378: + ":", "") == StringOptionPane.OK_OPTION) {
379: String name = stringOptionPane.getText();
380: if (name.length() > 0) {
381: try {
382: ContelligentComponent pastedComponent = (ContelligentComponent) clipData
383: .getTransferData(SingleComponentTransferable.componentFlavor);
384: String sourceEnvironment = (String) clipData
385: .getTransferData(SingleComponentTransferable.sourceEnvironmentFlavor);
386: ComponentMovement.nonBlockingCopy(getView()
387: .getEnvironment(),
388: sourceEnvironment, pastedComponent,
389: getComponent(), name, false, false);
390: } catch (UnsupportedFlavorException ee) {
391: ExceptionDialog.show(ee);
392: } catch (IOException ee) {
393: ExceptionDialog.show(ee);
394: }
395: }
396: }
397: }
398: }
399: }
400:
401: public class MoveUpAction extends AbstractAction implements
402: ContelligentAction {
403: public MoveUpAction() {
404: super ("move_up", Resources.moveUpIcon);
405: putValue(ROLLOVER_ICON, Resources.moveUpIconRollOver);
406: putValue(TYPE, PUSH_ACTION);
407: putValue(ACTION_TYPE, EDIT_ACTION);
408: putValue(MENU_TARGET, MENU);
409: putValue(BUTTON_TARGET, TOOLBAR);
410: }
411:
412: public void actionPerformed(ActionEvent e) {
413: int index = tabbedPane.getSelectedIndex();
414: String name = tabbedPane.getTitleAt(index);
415: try {
416: ContelligentComponent moveComponent = ComponentFactory
417: .getInstance().getComponent(
418: getComponent().getPath() + "/" + name);
419:
420: ActionResult result = Actions.sort(getView()
421: .getEnvironment(), moveComponent.getPath(),
422: Actions.UP);
423: result.showErrors();
424: } catch (RemoteActionException rae) {
425: ExceptionDialog.show(rae);
426: } catch (ComponentNotFoundException cnfe) {
427: ExceptionDialog.show(cnfe);
428: }
429: }
430: }
431:
432: public class MoveDownAction extends AbstractAction implements
433: ContelligentAction {
434:
435: public MoveDownAction() {
436: super ("move_down", Resources.moveDownIcon);
437: putValue(ROLLOVER_ICON, Resources.moveDownIconRollOver);
438: putValue(TYPE, PUSH_ACTION);
439: putValue(ACTION_TYPE, EDIT_ACTION);
440: putValue(MENU_TARGET, MENU);
441: putValue(BUTTON_TARGET, TOOLBAR);
442: }
443:
444: public void actionPerformed(ActionEvent e) {
445: int index = tabbedPane.getSelectedIndex();
446: String name = tabbedPane.getTitleAt(index);
447: try {
448: ContelligentComponent moveComponent = ComponentFactory
449: .getInstance().getComponent(
450: getComponent().getPath() + "/" + name);
451: ActionResult result = Actions.sort(getView()
452: .getEnvironment(), moveComponent.getPath(),
453: Actions.DOWN);
454: result.showErrors();
455: } catch (RemoteActionException rae) {
456: ExceptionDialog.show(rae);
457: } catch (ComponentNotFoundException cnfe) {
458: ExceptionDialog.show(cnfe);
459: }
460: }
461: }
462: }
|