001: package com.xoetrope.carousel.services.dialog;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.Dimension;
006: import java.awt.Rectangle;
007: import java.awt.event.ActionEvent;
008: import java.awt.event.ActionListener;
009: import javax.swing.JButton;
010: import javax.swing.JDialog;
011: import javax.swing.JPanel;
012: import javax.swing.JScrollPane;
013: import javax.swing.JSplitPane;
014: import javax.swing.JTable;
015: import javax.swing.JTextField;
016: import javax.swing.border.EmptyBorder;
017: import javax.swing.event.TreeSelectionEvent;
018: import javax.swing.event.TreeSelectionListener;
019: import javax.swing.table.DefaultTableModel;
020:
021: import net.xoetrope.editor.XEditorDefaults;
022: import net.xoetrope.builder.helper.XTableModelHelper; // import com.xoetrope.carousel.services.ProjectRouteManager;
023: import net.xoetrope.xui.XProjectManager;
024: import net.xoetrope.xui.data.XBaseModel;
025: import net.xoetrope.xui.data.XModel;
026: import javax.swing.SwingUtilities;
027: import net.xoetrope.xui.XProject;
028:
029: /**
030: * <p> Copyright (c) Xoetrope Ltd., 2001-2006, This software is licensed under
031: * the GNU Public License (GPL), please see license.txt for more details. If
032: * you make commercial use of this software you must purchase a commercial
033: * license from Xoetrope.</p>
034: * <p> $Revision: 1.7 $</p>
035: */
036: public class XLibDataChooser extends JDialog implements ActionListener,
037: TreeSelectionListener {
038: private JPanel mainPanel, pnlControls;
039: private JButton btnAdd, btnSave, btnCancel, btnOK, btnRefresh;
040: private boolean isOK = false;
041: private XLibDataTree dataTree;
042: private JTable valuesTable;
043: private DefaultTableModel listTableModel;
044: private JTextField txtStatus;
045: private JSplitPane splitPane = null;
046: private boolean isDebug;
047:
048: /**
049: * The owner project and the context in which this object operates.
050: */
051: protected XProject currentProject = XProjectManager
052: .getCurrentProject();
053:
054: public XLibDataChooser() {
055: this (false);
056: }
057:
058: public XLibDataChooser(boolean debug) {
059: isDebug = debug;
060:
061: mainPanel = new JPanel(new BorderLayout());
062: mainPanel.setBorder(new EmptyBorder(0, 0, 0, 0));
063: pnlControls = new JPanel(new BorderLayout());
064: pnlControls.setPreferredSize(new Dimension(230, 25));
065: pnlControls.setBorder(new EmptyBorder(0, 0, 0, 0));
066:
067: if (debug) {
068: btnRefresh = new JButton("Refresh");
069: btnRefresh.setFont(XEditorDefaults.defaultFont);
070: btnRefresh.addActionListener(this );
071: btnRefresh.setPreferredSize(new Dimension(230, 25));
072: pnlControls.add(btnRefresh, BorderLayout.CENTER);
073:
074: listTableModel = new DefaultTableModel();
075: listTableModel.addColumn("name");
076: listTableModel.addColumn("Value");
077: valuesTable = new JTable(listTableModel);
078:
079: JPanel rightPanel = new JPanel(new BorderLayout());
080: rightPanel.add(new JScrollPane(valuesTable),
081: BorderLayout.CENTER);
082: splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
083: mainPanel, rightPanel);
084: splitPane.setDividerSize(6);
085: getContentPane().setLayout(new BorderLayout());
086: getContentPane().add(splitPane, BorderLayout.CENTER);
087:
088: txtStatus = new JTextField();
089: txtStatus.setBackground(Color.LIGHT_GRAY);
090: txtStatus.setForeground(new Color(0, 0, 200));
091: getContentPane().add(txtStatus, BorderLayout.SOUTH);
092: setTitle("Model Explorer");
093: } else {
094: btnOK = new JButton("OK");
095: pnlControls.add(btnOK, BorderLayout.WEST);
096: btnOK.addActionListener(this );
097:
098: btnCancel = new JButton("Cancel");
099: pnlControls.add(btnCancel, BorderLayout.EAST);
100: btnCancel.addActionListener(this );
101: getContentPane().add(mainPanel);
102: }
103:
104: dataTree = new XLibDataTree(debug);
105: dataTree.dataTree.addTreeSelectionListener(this );
106: dataTree.expandRoot();
107:
108: mainPanel.add(dataTree, BorderLayout.CENTER);
109: mainPanel.add(pnlControls, BorderLayout.SOUTH);
110:
111: setModal(true);
112: setSize(640, 400);
113:
114: }
115:
116: public void valueChanged(TreeSelectionEvent evt) {
117: String path = getTreePath();
118: txtStatus.setText(path);
119: XModel mdl = (XModel) currentProject.getModel().get(path);
120:
121: listTableModel.setNumRows(0);
122:
123: for (int i = 0; i < mdl.getNumAttributes(); i++) {
124: String strValue = null;
125: if (mdl.getAttribValue(i) != null)
126: strValue = mdl.getAttribValue(i).toString();
127: String[] rowData = { mdl.getAttribName(i), strValue };
128: listTableModel.addRow(rowData);
129: }
130: }
131:
132: /**
133: * Get the path of the selected treenode by looping the selected path array
134: * and placing a '/' between each item
135: * @return the path to the selected XModel
136: */
137: String getTreePath() {
138: String modelName = null;
139: if (dataTree.dataTree.getSelectionPath() != null) {
140: Object path[] = dataTree.dataTree.getSelectionPath()
141: .getPath();
142:
143: modelName = "";
144: for (int i = 1; i < path.length; i++) {
145: String pathEle = path[i].toString();
146: pathEle = pathEle
147: .substring(0, pathEle.indexOf("~") - 1);
148: String value = path[i].toString();
149: value = value.substring(value.indexOf("~") + 3, value
150: .length() - 1);
151: if ((value.compareTo("null") != 0)
152: && (value.indexOf("DatabaseTableModel") == -1))
153: pathEle += "@value=[" + value + "]";
154: modelName += "/" + pathEle;
155: }
156: }
157: if (modelName == null)
158: return "";
159: else if (modelName.trim().compareTo("") != 0)
160: return modelName.substring(1);
161: else
162: return "";
163: }
164:
165: public void centerScreen(Rectangle parentRect) {
166: double x = parentRect.getWidth() - getSize().getWidth();
167: double y = parentRect.getHeight() - getSize().getHeight();
168: setLocation((int) x, (int) y);
169: }
170:
171: private void addData() {
172: XBaseModel model = (XBaseModel) currentProject.getModel();
173: XBaseModel rowdata = (XBaseModel) XTableModelHelper
174: .addRow(model);
175: XTableModelHelper.addData(rowdata, "event", "alert");
176: XTableModelHelper.addData(rowdata, "Control", "");
177: XTableModelHelper.addData(rowdata, "cmd", "info");
178: XTableModelHelper.addData(rowdata, "hash", "");
179: XTableModelHelper.addData(rowdata, "tag", "");
180: XTableModelHelper.addData(rowdata, "frame", "");
181: }
182:
183: public void actionPerformed(ActionEvent ae) {
184: if (ae.getSource().equals(btnOK)) {
185: isOK = true;
186: setVisible(false);
187: } else if (ae.getSource().equals(btnCancel)) {
188: isOK = false;
189: setVisible(false);
190: } else if (ae.getSource().equals(btnRefresh)) {
191: dataTree.createTreeComp(null);
192: dataTree.dataTree.addTreeSelectionListener(this );
193: }
194: }
195:
196: public void showDlg(String selected, boolean modal) {
197: setModal(false);
198: showDlg(selected);
199: }
200:
201: public String showDlg(String selected) {
202: //if ( isModal() )
203: // getModelManager().readXmlFile();
204: setVisible(true);
205:
206: if (isDebug) {
207: final JSplitPane splitter = splitPane;
208: SwingUtilities.invokeLater(new Runnable() {
209: public void run() {
210: validate();
211: splitter.setDividerLocation(0.3);
212: }
213: });
214: }
215:
216: if (isOK)
217: return dataTree.getTreePath();
218: else
219: return null;
220: }
221:
222: }
|