001: // THIS SOFTWARE IS PROVIDED BY SOFTARIS PTY.LTD. AND OTHER METABOSS
002: // CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
003: // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
004: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTARIS PTY.LTD.
005: // OR OTHER METABOSS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
006: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
007: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
008: // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
009: // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
010: // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
011: // EVEN IF SOFTARIS PTY.LTD. OR OTHER METABOSS CONTRIBUTORS ARE ADVISED OF THE
012: // POSSIBILITY OF SUCH DAMAGE.
013: //
014: // Copyright 2000-2005 © Softaris Pty.Ltd. All Rights Reserved.
015: package com.metaboss.applications.designstudio.complexfield;
016:
017: import java.awt.BorderLayout;
018: import java.awt.Dimension;
019: import java.awt.event.ActionEvent;
020: import java.awt.event.KeyAdapter;
021: import java.awt.event.KeyEvent;
022: import java.awt.event.MouseAdapter;
023: import java.awt.event.MouseEvent;
024:
025: import javax.swing.JButton;
026: import javax.swing.JLabel;
027: import javax.swing.JPanel;
028: import javax.swing.JScrollPane;
029: import javax.swing.JTextField;
030: import javax.swing.JToolBar;
031: import javax.swing.JTree;
032: import javax.swing.SwingUtilities;
033: import javax.swing.border.EmptyBorder;
034: import javax.swing.event.DocumentEvent;
035: import javax.swing.event.DocumentListener;
036: import javax.swing.event.TreeSelectionEvent;
037: import javax.swing.event.TreeSelectionListener;
038: import javax.swing.text.Position.Bias;
039: import javax.swing.tree.DefaultMutableTreeNode;
040: import javax.swing.tree.TreeModel;
041: import javax.swing.tree.TreePath;
042: import javax.swing.tree.TreeSelectionModel;
043:
044: import com.metaboss.applications.designstudio.Application;
045: import com.metaboss.applications.designstudio.BaseAction;
046: import com.metaboss.applications.designstudio.BasePropertiesDialog;
047: import com.metaboss.applications.designstudio.BaseUserObject;
048: import com.metaboss.applications.designstudio.designtree.DesignTreeTabDescriptor;
049:
050: /* Complex select dialog class */
051:
052: public class ComplexSelectDialog extends BasePropertiesDialog {
053: protected String mOldSearchText = null;
054: //ui
055: protected ComplexModel mModel = null;
056: protected JTree mTree = null;
057: protected JPanel mSearchPanel = new JPanel();
058: protected JTextField mSearchTextField = new JTextField();
059: protected JToolBar mSearchToolBar = new JToolBar();
060: //actions
061: protected SearchDownAction mSearchDownAction = new SearchDownAction();
062: protected SearchUpAction mSearchUpAction = new SearchUpAction();
063: protected ClearAction mClearAction = new ClearAction();
064:
065: public ComplexSelectDialog(String pCaption, String pSelectText,
066: Dimension pDimension) {
067: super (pSelectText, true, pDimension);
068:
069: if (pCaption == null)
070: mCaptionLabel.setText(pSelectText);
071: else
072: mCaptionLabel.setText(pCaption);
073:
074: mClient.setLayout(new BorderLayout());
075: mClient.setBorder(new EmptyBorder(3, 3, 0, 3));
076:
077: mModel = createTreeModel();
078: mTree = new SearchTree(mModel);
079:
080: mTree.getSelectionModel().setSelectionMode(
081: TreeSelectionModel.SINGLE_TREE_SELECTION);
082: mTree.setShowsRootHandles(true);
083: mTree.setRootVisible(false);
084: mTree.setCellRenderer(DesignTreeTabDescriptor.sRenderer);
085:
086: mTree.addTreeSelectionListener(new TreeSelectionListener() {
087: public void valueChanged(TreeSelectionEvent e) {
088: setButtonsState();
089: }
090: });
091: mTree.addMouseListener(new MouseAdapter() {
092: public void mousePressed(MouseEvent e) {
093: if (SwingUtilities.isLeftMouseButton(e)) {
094: BaseUserObject lDataType = getSelectedObject();
095: if (lDataType != null) {
096: switch (e.getClickCount()) {
097: case 2:
098: try {
099: mOKAction.run();
100: } catch (Exception ex) {
101: Application.processError(ex);
102: }
103: break;
104: }
105: }
106: }
107: super .mousePressed(e);
108: }
109: });
110:
111: mSearchPanel.setLayout(new BorderLayout());
112:
113: mSearchTextField.getDocument().addDocumentListener(
114: new SearchFieldListener());
115: mSearchTextField.addKeyListener(new KeyAdapter() {
116: public void keyReleased(KeyEvent e) {
117: super .keyReleased(e);
118: searchChanged();
119: }
120: });
121:
122: mSearchPanel.add(mSearchTextField, BorderLayout.CENTER);
123: mSearchPanel.add(mSearchToolBar, BorderLayout.EAST);
124: mSearchPanel.add(new JLabel("Search: "), BorderLayout.WEST);
125:
126: mSearchToolBar.setBorder(null);
127: mSearchToolBar.add(Application.createButton(mSearchDownAction));
128: mSearchToolBar.add(Application.createButton(mSearchUpAction));
129:
130: mClient.add(new JScrollPane(mTree));
131: mClient.add(mSearchPanel, BorderLayout.SOUTH);
132:
133: setResizable(true);
134: setButtonsState();
135: setSearchActionsState();
136:
137: mSearchTextField.requestFocusInWindow();
138: mButtonBarGrid.add(new JButton(mClearAction));
139: }
140:
141: protected ComplexModel createTreeModel() {
142: return null;
143: }
144:
145: protected void setButtonsState() {
146: //???
147: }
148:
149: // get current user object
150: protected BaseUserObject getCurrentUserObject() {
151: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) mTree
152: .getLastSelectedPathComponent();
153: Object lObject = (lNode != null) ? lNode.getUserObject() : null;
154: return (lObject != null && lObject instanceof BaseUserObject) ? (BaseUserObject) lObject
155: : null;
156: }
157:
158: // SerachBox changed
159: protected void searchChanged() {
160: String lNewText = mSearchTextField.getText();
161: if (lNewText != null && lNewText.length() > 0
162: && !lNewText.equals(mOldSearchText)) {
163: DefaultMutableTreeNode lNode = mModel
164: .findChildNodeByPartCaption(lNewText, true);
165: if (lNode != null) {
166: mModel.selectNode(lNode, mTree);
167: mOldSearchText = lNewText;
168: } else {
169: if (mOldSearchText != null)
170: mSearchTextField.setText(mOldSearchText);
171: else
172: mSearchTextField.setText("");
173: }
174: }
175: }
176:
177: protected void findNextNode() {
178: String lText = mSearchTextField.getText();
179: if (lText != null && lText.length() > 0) {
180: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) mTree
181: .getLastSelectedPathComponent();
182: if (lNode != null) {
183: lNode = mModel.findNext(lNode, lText);
184: if (lNode != null)
185: mModel.selectNode(lNode, mTree);
186: }
187: }
188: }
189:
190: protected void findPrevNode() {
191: String lText = mSearchTextField.getText();
192: if (lText != null && lText.length() > 0) {
193: DefaultMutableTreeNode lNode = (DefaultMutableTreeNode) mTree
194: .getLastSelectedPathComponent();
195: if (lNode != null) {
196: lNode = mModel.findPrev(lNode, lText);
197: if (lNode != null)
198: mModel.selectNode(lNode, mTree);
199: }
200: }
201: }
202:
203: // set Search Actions State
204: protected void setSearchActionsState() {
205: String lNewText = mSearchTextField.getText();
206: boolean lEnabled = lNewText != null && lNewText.length() > 0;
207:
208: mSearchDownAction.setEnabled(lEnabled);
209: mSearchUpAction.setEnabled(lEnabled);
210: }
211:
212: protected void clear() {
213: mModel.selectNode((DefaultMutableTreeNode) null, mTree);
214: ok();
215: }
216:
217: /* Properties */
218:
219: public BaseUserObject getSelectedObject() {
220: return null;
221: }
222:
223: public void setSelectedObject(BaseUserObject pObject) {
224: mModel.selectNode(pObject, mTree);
225: }
226:
227: /* Auxilary classes */
228:
229: public class SearchFieldListener implements DocumentListener {
230: public void changedUpdate(DocumentEvent e) {
231: //???
232: }
233:
234: public void insertUpdate(DocumentEvent e) {
235: setSearchActionsState();
236: }
237:
238: public void removeUpdate(DocumentEvent e) {
239: setSearchActionsState();
240: }
241: }
242:
243: public class SearchTree extends JTree {
244: public SearchTree(TreeModel pModel) {
245: super (pModel);
246: }
247:
248: public TreePath getNextMatch(String prefix, int startingRow,
249: Bias bias) {
250: //return super.getNextMatch(prefix, startingRow, bias);
251: String lText = mSearchTextField.getText() + prefix;
252: mSearchTextField.setText(lText);
253: searchChanged();
254: return null;
255: }
256: }
257:
258: /* Actions */
259:
260: public class SearchDownAction extends BaseAction {
261: public SearchDownAction() {
262: super ("Search Down", Application.SEARCHDOWN_ICON);
263: }
264:
265: public void actionPerformed(ActionEvent arg0) {
266: findNextNode();
267: }
268: }
269:
270: public class SearchUpAction extends BaseAction {
271: public SearchUpAction() {
272: super ("Search Up", Application.SEARCHUP_ICON);
273: }
274:
275: public void actionPerformed(ActionEvent arg0) {
276: findPrevNode();
277: }
278: }
279:
280: public class ClearAction extends BaseAction {
281: public ClearAction() {
282: super ("Clear", null, null);
283: }
284:
285: public void actionPerformed(ActionEvent arg0) {
286: clear();
287: }
288: }
289: }
|