001: package org.enhydra.jawe.base.panel;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Component;
005: import java.util.ArrayList;
006: import java.util.EventObject;
007: import java.util.HashMap;
008: import java.util.List;
009: import java.util.Map;
010:
011: import javax.swing.BorderFactory;
012: import javax.swing.Box;
013: import javax.swing.BoxLayout;
014: import javax.swing.JComponent;
015: import javax.swing.JDialog;
016: import javax.swing.JLabel;
017: import javax.swing.JOptionPane;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.JToolBar;
021: import javax.swing.JViewport;
022: import javax.swing.ScrollPaneConstants;
023:
024: import org.enhydra.jawe.BarFactory;
025: import org.enhydra.jawe.HistoryManager;
026: import org.enhydra.jawe.JaWEComponent;
027: import org.enhydra.jawe.JaWEComponentView;
028: import org.enhydra.jawe.JaWEManager;
029: import org.enhydra.jawe.Settings;
030: import org.enhydra.jawe.XPDLElementChangeInfo;
031: import org.enhydra.jawe.base.controller.JaWESelectionManager;
032: import org.enhydra.jawe.base.display.DisplayNameGenerator;
033: import org.enhydra.jawe.base.label.LabelGenerator;
034: import org.enhydra.jawe.base.panel.panels.XMLBasicPanel;
035: import org.enhydra.jawe.base.panel.panels.XMLPanel;
036: import org.enhydra.jawe.base.panel.panels.XMLTabbedPanel;
037: import org.enhydra.jawe.base.tooltip.TooltipGenerator;
038: import org.enhydra.shark.xpdl.XMLComplexChoice;
039: import org.enhydra.shark.xpdl.XMLElement;
040: import org.enhydra.shark.xpdl.XMLElementChangeInfo;
041: import org.enhydra.shark.xpdl.XMLUtil;
042:
043: public class InlinePanel extends JPanel implements JaWEComponentView,
044: PanelContainer {
045:
046: protected JaWEComponent controller;
047:
048: protected JScrollPane scrollPane;
049:
050: protected boolean displayTitle = false;
051:
052: protected JLabel title;
053:
054: protected boolean isModified = false;
055:
056: protected PanelGenerator panelGenerator;
057:
058: protected Map lastActiveTabs = new HashMap();
059:
060: protected HistoryManager hm;
061:
062: public void configure() {
063: }
064:
065: public InlinePanel() {
066: }
067:
068: public void setJaWEComponent(JaWEComponent jc) {
069: this .controller = jc;
070: }
071:
072: public PanelSettings getPanelSettings() {
073: return (PanelSettings) this .controller.getSettings();
074: }
075:
076: public void init() {
077:
078: ClassLoader cl = getClass().getClassLoader();
079: try {
080: this .panelGenerator = (PanelGenerator) cl.loadClass(
081: JaWEManager.getInstance()
082: .getPanelGeneratorClassName())
083: .newInstance();
084: } catch (Exception ex) {
085: String msg = "InlinePanel -> Problems while instantiating Panel Generator class '"
086: + JaWEManager.getInstance()
087: .getPanelGeneratorClassName()
088: + "' - using default implementation!";
089:
090: JaWEManager.getInstance().getLoggingManager()
091: .error(msg, ex);
092: this .panelGenerator = new StandardPanelGenerator();
093: }
094: this .panelGenerator.setPanelContainer(this );
095:
096: try {
097: String hmc = getPanelSettings().historyManagerClass();
098: if (hmc != null && !hmc.equals("")) {
099: hm = (HistoryManager) Class.forName(hmc).newInstance();
100: hm.init(getPanelSettings().historySize());
101: }
102: } catch (Exception ex) {
103: System.err
104: .println("Failed to instantiate history manager - my controller is "
105: + controller);
106: }
107:
108: getPanelSettings().adjustActions();
109:
110: displayTitle = ((Boolean) controller.getSettings().getSetting(
111: "DisplayTitle")).booleanValue();
112:
113: // creates scroll panel
114: scrollPane = new JScrollPane();
115: if (((Boolean) controller.getSettings().getSetting(
116: "UseScrollBar")).booleanValue()) {
117: scrollPane
118: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
119: scrollPane
120: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
121: } else {
122: scrollPane
123: .setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
124: scrollPane
125: .setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
126: }
127:
128: JViewport port = scrollPane.getViewport();
129: port.setScrollMode(JViewport.BLIT_SCROLL_MODE);
130: scrollPane.getVerticalScrollBar().setUnitIncrement(20);
131: scrollPane.getHorizontalScrollBar().setUnitIncrement(40);
132:
133: setBorder(BorderFactory.createEtchedBorder());
134: setLayout(new BorderLayout());
135:
136: JPanel wp = new JPanel();
137: JToolBar toolbar = BarFactory.createToolbar("defaultToolbar",
138: controller);
139: toolbar.setFloatable(false);
140: toolbar.setRollover(true);
141: wp.setLayout(new BoxLayout(wp, BoxLayout.Y_AXIS));
142:
143: wp.add(Box.createVerticalStrut(5));
144: if (displayTitle) {
145: JPanel p = new JPanel();
146: p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
147: title = new JLabel("");
148: title.setAlignmentX(Component.LEFT_ALIGNMENT);
149: title.setAlignmentY(Component.BOTTOM_ALIGNMENT);
150: // p.add(Box.createHorizontalGlue());
151: p.add(title);
152: p.add(Box.createHorizontalGlue());
153: wp.add(p);
154: }
155:
156: wp.add(scrollPane);
157:
158: add(toolbar, BorderLayout.NORTH);
159: add(wp, BorderLayout.CENTER);
160: }
161:
162: public JaWEComponent getJaWEComponent() {
163: return controller;
164: }
165:
166: public JComponent getDisplay() {
167: return this ;
168: }
169:
170: public void update(XPDLElementChangeInfo info) {
171: if (info.getSource() == this ) {
172: return;
173: }
174:
175: XMLElement changedElement = info.getChangedElement();
176: XMLElement current = getActiveElement();
177: List removedElements = new ArrayList();
178:
179: JaWESelectionManager jsm = JaWEManager.getInstance()
180: .getJaWEController().getSelectionManager();
181: if (changedElement == null
182: || (jsm.getSelectedElements().size() == 1 && !jsm
183: .canEditProperties())) {
184: setActiveElement(null);
185: } else if (info.getAction() == XMLElementChangeInfo.UPDATED) {
186: if (current != null
187: && XMLUtil.isChildsParent(current, info
188: .getChangedElement())) {
189: setActiveElement(current);
190: }
191: } else if (info.getAction() == XPDLElementChangeInfo.SELECTED) {
192: setActiveElement(changedElement);
193: // TODO: send multi sel to XMLTable and XMLList panels
194: // if (changedElement instanceof XMLCollection) {
195: // List chngdSubEls=info.getChangedSubElements();
196: // if (chngdSubEls.size()>0) {
197: // }
198: // }
199: } else if (info.getAction() == XMLElementChangeInfo.REMOVED) {
200: List l = info.getChangedSubElements();
201: if (l == null || l.size() == 0) {
202: l = new ArrayList();
203: l.add(info.getChangedElement());
204: }
205: for (int i = 0; i < l.size(); i++) {
206: XMLElement el = (XMLElement) l.get(i);
207: if (el == current
208: || XMLUtil.isParentsChild(el, current)) {
209: setActiveElement(null);
210: }
211: removedElements.add(el);
212: }
213: }
214:
215: if (hm != null) {
216: for (int i = 0; i < removedElements.size(); i++) {
217: hm.removeFromHistory((XMLElement) removedElements
218: .get(i));
219: }
220: }
221:
222: getPanelSettings().adjustActions();
223: }
224:
225: public void setViewPanel(XMLPanel panel) {
226: XMLPanel current = getViewPanel();
227: if (current != null) {
228: current.cleanup();
229: }
230: if (displayTitle) {
231: XMLElement el = panel.getOwner();
232: String t = "";
233: if (el != null) {
234: // t = " " + getLabelGenerator().getLabel(el);
235: t = " " + panel.getTitle();
236: }
237: title.setText(t);
238: }
239:
240: this .scrollPane.setViewportView(panel);
241: }
242:
243: public XMLPanel getViewPanel() {
244: if (scrollPane != null)
245: return (XMLPanel) this .scrollPane.getViewport().getView();
246:
247: return null;
248: }
249:
250: public XMLElement getActiveElement() {
251: XMLPanel p = getViewPanel();
252: if (p != null) {
253: XMLElement current = p.getOwner();
254: if (current instanceof SpecialChoiceElement) {
255: current = ((SpecialChoiceElement) current)
256: .getControlledElement();
257: } else if (current instanceof ActivityTypesChoiceElement) {
258: current = ((ActivityTypesChoiceElement) current)
259: .getControlledElement();
260: }
261: return current;
262: }
263: return null;
264: }
265:
266: public void apply() {
267: XMLPanel p = getViewPanel();
268: if (p != null) {
269: p.setElements();
270: }
271: }
272:
273: public boolean canApplyChanges() {
274: if (getViewPanel() != null) {
275: XMLPanel p = getViewPanel();
276: // System.err.println("CAAAAAAAACCCCCCCC for "+p);
277: if (p.validateEntry()) {
278: // System.err.println("ENTRY IS VALID FOR "+p);
279: return JaWEManager.getInstance().getPanelValidator()
280: .validatePanel(p.getOwner(), p);
281: }
282: }
283:
284: return false;
285: }
286:
287: public boolean validateElement(XMLElement el) {
288: // boolean retVal = true;
289: // XPDLValidator xpdlValidator =
290: // JaWEManager.getInstance().getXPDLValidator();
291: //
292: // xpdlValidator.init(JaWE.getInstance().getProperties(),
293: // JaWEManager.getInstance()
294: // .getXPDLHandler(),
295: // JaWEManager.getInstance().getJaWEController().getMainPackage(),
296: // true, true, true, true,
297: // JaWEManager.getInstance().getJaWEController().getEncoding());
298: //
299: // if (el instanceof Activity
300: // && ((Activity) el).getActivityType() ==
301: // XPDLConstants.ACTIVITY_TYPE_SUBFLOW) {
302: // try {
303: // boolean isSubFlowOK = xpdlValidator.checkActivitySubFlow((Activity) el,
304: // true);
305: // if (!isSubFlowOK) {
306: // JOptionPane.showMessageDialog(JaWEManager.getInstance().getJaWEController().getJaWEFrame(),
307: // JaWEManager.getInstance().getXPDLElementEditor().getLanguageDependentString(
308: // "ErrorSubFlowFormalAndActualParametersDoNotMatch"), JaWEManager
309: // .getInstance().getXPDLElementEditor().getLanguageDependentString(
310: // "ErrorMessageKey"), JOptionPane.WARNING_MESSAGE);
311: // retVal = false;
312: // }
313: // } catch (Throwable e) {
314: // retVal = false;
315: // }
316: // }
317: // return retVal;
318: return true;
319: }
320:
321: public void applySpecial() {
322: if (!canApplyChanges())
323: return;
324: XMLElement el = getActiveElement();
325:
326: getJaWEComponent().setUpdateInProgress(true);
327: JaWEManager.getInstance().getJaWEController()
328: .startUndouableChange();
329: apply();
330: List toSelect = new ArrayList();
331: XMLElement toSel = el;
332: if (toSel != null) {
333: toSelect.add(toSel);
334: }
335: JaWEManager.getInstance().getJaWEController()
336: .endUndouableChange(toSelect);
337: getJaWEComponent().setUpdateInProgress(false);
338:
339: validateElement(el);
340:
341: setModified(false);
342: }
343:
344: public void displayParentPanel() {
345: if (isModified) {
346: int sw = showModifiedWarning();
347: if (sw == JOptionPane.CANCEL_OPTION
348: || (sw == JOptionPane.YES_OPTION && isModified()))
349: return;
350: }
351: XMLElement el = getActiveElement();
352: if (el != null) {
353: XMLElement parent = el.getParent();
354: if (parent != null) {
355: XMLElement choice = null;
356: while ((choice = XMLUtil
357: .getParentElementByAssignableType(
358: XMLComplexChoice.class, parent)) != null) {
359: parent = choice.getParent();
360: }
361: setActiveElement(parent);
362: JaWEManager.getInstance().getJaWEController()
363: .getSelectionManager().setSelection(parent,
364: true);
365: }
366: }
367: }
368:
369: public void displayGivenElement(XMLElement el) {
370: if (isModified) {
371: int sw = showModifiedWarning();
372: if (sw == JOptionPane.CANCEL_OPTION
373: || (sw == JOptionPane.YES_OPTION && isModified()))
374: return;
375: }
376: if (el != null) {
377: setActiveElement(el);
378: JaWEManager.getInstance().getJaWEController()
379: .getSelectionManager().setSelection(el, true);
380: if (getJaWEComponent() instanceof JDialog
381: && ((JDialog) getJaWEComponent()).isModal()) {
382: ((JDialog) getJaWEComponent())
383: .setTitle(JaWEManager.getInstance()
384: .getLabelGenerator().getLabel(el));
385: }
386: }
387: }
388:
389: public void displayElement(XMLElement el) {
390: XMLPanel previousPanel = getViewPanel();
391: XMLElement previousElement = getActiveElement();
392: if (previousPanel instanceof XMLTabbedPanel
393: && previousElement != null) {
394: XMLTabbedPanel tp = (XMLTabbedPanel) previousPanel;
395: Class ec = previousElement.getClass();
396: int activeTab = tp.getActiveTab();
397: lastActiveTabs.put(ec, new Integer(activeTab));
398: }
399:
400: // MUST BE SET BEFORE GENERATING NEW PANEL BECAUSE PANELGENERATOR
401: // CAN SET THIS FLAG TO TRUE
402: isModified = false;
403:
404: XMLPanel p;
405: if (el != null) {
406: p = this .panelGenerator.getPanel(el);
407: } else {
408: p = new XMLBasicPanel();
409: }
410: setViewPanel(p);
411: if (p instanceof XMLTabbedPanel) {
412: Integer at = (Integer) lastActiveTabs.get(el.getClass());
413: if (at != null) {
414: int atno = at.intValue();
415: XMLTabbedPanel tp = (XMLTabbedPanel) p;
416: if (tp.getTabCount() <= at.intValue()) {
417: atno = tp.getTabCount() - 1;
418: lastActiveTabs
419: .put(el.getClass(), new Integer(atno));
420: }
421: tp.setActiveTab(atno);
422: }
423: }
424: getPanelSettings().adjustActions();
425: // enableApplyAction(isModified);
426: // enableRevertAction(isModified);
427:
428: }
429:
430: public void setActiveElement(XMLElement el) {
431: if (hm != null) {
432: XMLElement current = getActiveElement();
433: hm.addToHistory(current, el);
434: }
435: displayElement(el);
436: getPanelSettings().adjustActions();
437: }
438:
439: public void displayPreviousElement() {
440: if (hm == null)
441: return;
442:
443: if (isModified()) {
444: int sw = showModifiedWarning();
445: if (sw == JOptionPane.CANCEL_OPTION
446: || (sw == JOptionPane.YES_OPTION && isModified()))
447: return;
448: }
449: if (hm.canGoBack()) {
450: XMLElement el = hm.getPrevious(getActiveElement());
451: displayElement(el);
452: JaWEManager.getInstance().getJaWEController()
453: .getSelectionManager().setSelection(el, true);
454: getPanelSettings().adjustActions();
455: }
456: }
457:
458: public void displayNextElement() {
459: if (hm == null)
460: return;
461:
462: if (isModified()) {
463: int sw = showModifiedWarning();
464: if (sw == JOptionPane.CANCEL_OPTION
465: || (sw == JOptionPane.YES_OPTION && isModified()))
466: return;
467: }
468: if (hm.canGoForward()) {
469: XMLElement el = hm.getNext(getActiveElement());
470: displayElement(el);
471:
472: JaWEManager.getInstance().getJaWEController()
473: .getSelectionManager().setSelection(el, true);
474: getPanelSettings().adjustActions();
475: }
476: }
477:
478: public int showModifiedWarning() {
479: if (!getPanelSettings().shouldShowModifiedWarning())
480: return JOptionPane.NO_OPTION;
481: int option = JOptionPane.showConfirmDialog(JaWEManager
482: .getInstance().getJaWEController().getJaWEFrame(),
483: getPanelSettings().getLanguageDependentString(
484: "WarningElementChanged"), getPanelSettings()
485: .getLanguageDependentString("DialogTitle"),
486: JOptionPane.YES_NO_CANCEL_OPTION);
487: if (option == JOptionPane.YES_OPTION) {
488: applySpecial();
489: }
490: return option;
491: }
492:
493: public void setModified(boolean isModified) {
494: this .isModified = isModified;
495:
496: // enableApplyAction(isModified);
497: // enableRevertAction(isModified);
498: getPanelSettings().adjustActions();
499:
500: }
501:
502: public boolean isModified() {
503: return this .isModified;
504: }
505:
506: public void cleanup() {
507: if (hm != null) {
508: hm.cleanHistory();
509: }
510: getPanelSettings().adjustActions();
511: }
512:
513: public void panelChanged(XMLPanel panel, EventObject ev) {
514: isModified = true;
515: // enableApplyAction(isModified);
516: // enableRevertAction(isModified);
517: getPanelSettings().adjustActions();
518:
519: }
520:
521: public Settings getSettings() {
522: return controller.getSettings();
523: }
524:
525: public String getLanguageDependentString(String nm) {
526: return controller.getSettings().getLanguageDependentString(nm);
527: }
528:
529: public PanelGenerator getPanelGenerator() {
530: return panelGenerator;
531: }
532:
533: public LabelGenerator getLabelGenerator() {
534: return JaWEManager.getInstance().getLabelGenerator();
535: }
536:
537: public DisplayNameGenerator getDisplayNameGenerator() {
538: return JaWEManager.getInstance().getDisplayNameGenerator();
539: }
540:
541: public PanelValidator getPanelValidator() {
542: return JaWEManager.getInstance().getPanelValidator();
543: }
544:
545: public TooltipGenerator getTooltipGenerator() {
546: return JaWEManager.getInstance().getTooltipGenerator();
547: }
548:
549: public HistoryManager getHistoryManager() {
550: return hm;
551: }
552:
553: }
|