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.event.ActionEvent;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.logging.Level;
024:
025: import javax.swing.AbstractAction;
026: import javax.swing.Action;
027: import javax.swing.JOptionPane;
028: import javax.swing.tree.DefaultMutableTreeNode;
029: import javax.swing.tree.TreePath;
030:
031: import de.finix.contelligent.client.base.ComponentFactory;
032: import de.finix.contelligent.client.base.ComponentNotFoundException;
033: import de.finix.contelligent.client.base.ComponentReference;
034: import de.finix.contelligent.client.base.ContelligentComponent;
035: import de.finix.contelligent.client.gui.ComponentEditor;
036: import de.finix.contelligent.client.gui.ContelligentAction;
037: import de.finix.contelligent.client.gui.category.CategorySensitiveComposedEditor;
038: import de.finix.contelligent.client.gui.directory.FindOptionPane;
039: import de.finix.contelligent.client.gui.text.ContelligentEditorKit;
040: import de.finix.contelligent.client.gui.text.ReplaceDialog;
041: import de.finix.contelligent.client.gui.text.TextEditor;
042: import de.finix.contelligent.client.i18n.Resources;
043: import de.finix.contelligent.client.remote.ActionResult;
044: import de.finix.contelligent.client.remote.Actions;
045: import de.finix.contelligent.client.remote.RemoteActionException;
046: import de.finix.contelligent.client.util.ExceptionDialog;
047:
048: public class FindTextAction extends AbstractAction implements
049: ContelligentAction {
050: /**
051: *
052: */
053: private final ExplorerEditor editor;
054:
055: public FindTextAction(ExplorerEditor editor) {
056: super ("find_text_action", Resources.findIcon);
057: this .editor = editor;
058: putValue(TYPE, PUSH_ACTION);
059: putValue(ACTION_TYPE, EDIT_ACTION);
060: putValue(ACTION_GROUP, EDIT_SEARCH_GROUP);
061: putValue(ACTION_POS, EDIT_SEARCH_TEXT);
062: putValue(MENU_TARGET, MENU);
063: putValue(BUTTON_TARGET, NO_BUTTON);
064: putValue(POPUP_TARGET, POPUP_SUBMENU);
065: putValue(POPUP_PARENT_ACTION, this .editor.findAction);
066: }
067:
068: public void actionPerformed(ActionEvent e) {
069: final TreePath[] selectionPaths = this .editor.tree
070: .getSelectionPaths();
071: String findPath = null;
072: if (selectionPaths != null) {
073: if (selectionPaths.length > 0
074: && this .editor.guiContainer != null) {
075: DefaultMutableTreeNode firstNode = ((DefaultMutableTreeNode) selectionPaths[0]
076: .getLastPathComponent());
077: if (firstNode != null
078: && firstNode.getUserObject() instanceof ComponentReference) {
079: try {
080: ContelligentComponent component = ((ComponentReference) firstNode
081: .getUserObject()).getComponent();
082: findPath = component.getPath();
083: } catch (ComponentNotFoundException cnfe) {
084: ExceptionDialog.show(cnfe);
085: }
086: }
087: }
088: }
089: FindOptionPane findOptionPane = new FindOptionPane();
090: if (findOptionPane.showFindDialog(this .editor.searchTerm,
091: findPath, false) == JOptionPane.OK_OPTION) {
092: try {
093: this .editor.searchTerm = findOptionPane.getSearchText();
094: if (findOptionPane.searchAll()) {
095: findPath = "/";
096: } else {
097: findPath = findOptionPane.getPath();
098: }
099: ActionResult response = Actions.find(editor.getView()
100: .getEnvironment(), this .editor.searchTerm,
101: findPath, ComponentFactory.getInstance()
102: .getCurrentContext().getName());
103: ContelligentEditorKit
104: .setSearchString(this .editor.searchTerm);
105: final Collection paths = response.getPaths();
106: if (this .editor.pathSelectionManager != null) {
107: this .editor.pathSelectionManager
108: .setPaths(Resources
109: .getLocalString("search_results"),
110: paths,
111: new Action[] { new AbstractAction(
112: "replace") {
113: public void actionPerformed(
114: ActionEvent e) {
115: ReplaceDialog dialog = new ReplaceDialog(
116: ContelligentEditorKit
117: .getSearchString(),
118: ContelligentEditorKit
119: .getReplaceString(),
120: false);
121: int option;
122: if ((option = dialog
123: .showReplaceDialog()) != JOptionPane.CANCEL_OPTION
124: && getSearchString(dialog)) {
125: String selectedPath = FindTextAction.this .editor.pathSelectionManager
126: .getSelectedPath();
127: boolean start = (selectedPath == null);
128: for (Iterator it = paths
129: .iterator(); it
130: .hasNext();) {
131: String path = (String) it
132: .next();
133: if (!start
134: && !path
135: .equals(selectedPath)) {
136: continue;
137: } else {
138: start = true;
139: FindTextAction.this .editor
140: .selectComponent(path);
141: try {
142: ContelligentComponent component = ComponentFactory
143: .getInstance()
144: .getComponent(
145: path);
146: FindTextAction.this .editor.guiContainer
147: .setSelectedComponent(component);
148: FindTextAction.this .editor.guiContainer
149: .renderSelectedComponent();
150: FindTextAction.this .editor
151: .selectComponent(component);
152: FindTextAction.this .editor.guiContainer
153: .editSelectedComponent();
154: ComponentEditor[] editor = FindTextAction.this .editor
155: .getView()
156: .getEditors();
157: for (int i = 0; i < editor.length; i++) {
158: if (editor[i] instanceof CategorySensitiveComposedEditor) {
159: FindTextAction.this .editor
160: .getView()
161: .makeEditorVisible(
162: editor[i]);
163: for (Iterator ie = ((CategorySensitiveComposedEditor) editor[i])
164: .getEditors()
165: .iterator(); ie
166: .hasNext();) {
167: ComponentEditor innerEditor = (ComponentEditor) ie
168: .next();
169: if (innerEditor instanceof TextEditor) {
170: ContelligentEditorKit
171: .setSearchString(dialog
172: .getSearchText());
173: ContelligentEditorKit
174: .setReplaceString(dialog
175: .getReplaceText());
176: option = ((TextEditor) innerEditor)
177: .getEditorKit()
178: .replace(
179: 0,
180: option);
181: innerEditor
182: .commit();
183: }
184: }
185: }
186: }
187: } catch (ComponentNotFoundException cnfe) {
188: ExplorerEditor.logger
189: .log(
190: Level.WARNING,
191: "Could not search in component!",
192: cnfe);
193: }
194: }
195: FindTextAction.this .editor
196: .getView()
197: .commit();
198: }
199: }
200: }
201: } });
202: this .editor.pathSelectionManager.showPaths();
203: }
204: } catch (RemoteActionException rae) {
205: ExceptionDialog.show(rae);
206: }
207: }
208: }
209:
210: private boolean getSearchString(ReplaceDialog replaceDialog) {
211: if (replaceDialog == null)
212: return false;
213: ContelligentEditorKit.setSearchString(replaceDialog
214: .getSearchText());
215: ContelligentEditorKit.setReplaceString(replaceDialog
216: .getReplaceText());
217: if (ContelligentEditorKit.getSearchString().length() == 0) {
218: java.awt.Toolkit.getDefaultToolkit().beep();
219: return false;
220: }
221: return true;
222: }
223: }
|