001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.cleverphl.gui;
025:
026: import com.jgoodies.forms.builder.PanelBuilder;
027: import com.jgoodies.forms.layout.CellConstraints;
028: import com.jgoodies.forms.layout.FormLayout;
029:
030: import jacareto.cleverphl.CleverPHL;
031: import jacareto.system.Language;
032:
033: import org.apache.regexp.RE;
034: import org.apache.regexp.RESyntaxException;
035:
036: import java.awt.BorderLayout;
037: import java.awt.FlowLayout;
038: import java.awt.Point;
039: import java.awt.event.ActionEvent;
040: import java.awt.event.ActionListener;
041:
042: import javax.swing.ButtonGroup;
043: import javax.swing.JButton;
044: import javax.swing.JCheckBox;
045: import javax.swing.JComponent;
046: import javax.swing.JDialog;
047: import javax.swing.JPanel;
048: import javax.swing.JRadioButton;
049: import javax.swing.JTextField;
050:
051: /**
052: * Dialog for the search.
053: *
054: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
055: * @version 1.0
056: */
057: public class FindDialog extends JDialog implements ActionListener {
058: public static final int SCOPE_ACTUAL_SESSION = 0;
059: public static final int SCOPE_ALL_SESSIONS = 1;
060:
061: /** The CleverPHL instance. */
062: private CleverPHL cleverPHL;
063:
064: /** The regular expression field. */
065: private JTextField findwhatField;
066:
067: /** The button testing the regexp */
068: private JButton regexpTestButton;
069: private JCheckBox regexpCheckBox;
070:
071: /** JRadiobuttons for the scope. */
072: private JRadioButton scopeActualSessionButton;
073: private JRadioButton scopeAllSessionsButton;
074:
075: /** The buttons. */
076: private JButton okButton;
077:
078: /** The buttons. */
079: private JButton cancelButton;
080:
081: /** The results. */
082: private boolean okButtonPressed;
083: private String searchString;
084: private boolean isRegExp;
085: private int scope;
086:
087: /**
088: * Creates a new dialog (visible).
089: *
090: * @param cleverPHL the CleverPHL instance
091: */
092: public FindDialog(CleverPHL cleverPHL) {
093: super (cleverPHL.getMainFrame(), true);
094: this .cleverPHL = cleverPHL;
095: setName(cleverPHL.getCustomization().getString(
096: "Components.JacaretoComponent", "JACARETO_COMPONENT"));
097:
098: Language language = cleverPHL.getLanguage();
099:
100: //setLocationRelativeTo ( cleverPHL.getMainFrame() );
101: setTitle(language.getString("CleverPHL.FindDialog.Title"));
102:
103: okButton = new JButton(language.getString("General.Ok"));
104: cancelButton = new JButton(language.getString("General.Cancel"));
105: okButton.addActionListener(this );
106: cancelButton.addActionListener(this );
107:
108: JPanel buttonPanel = new JPanel(new FlowLayout(
109: FlowLayout.CENTER));
110: buttonPanel.add(okButton);
111: buttonPanel.add(cancelButton);
112:
113: getContentPane().setLayout(new BorderLayout());
114: getContentPane().add(getSearchPanel(), BorderLayout.NORTH);
115: getContentPane().add(buttonPanel, BorderLayout.CENTER);
116:
117: Point ownerLoc = cleverPHL.getMainFrame().getLocation();
118: setLocation(((int) ownerLoc.getX()) + 30, ((int) ownerLoc
119: .getY()) + 30);
120:
121: pack();
122: setResizable(false);
123: setVisible(true);
124: }
125:
126: /**
127: * Returns the main panel of the dialog.
128: *
129: * @return the component
130: */
131: private JComponent getSearchPanel() {
132: FormLayout layout = new FormLayout("10dlu,p,3dlu,left:p",
133: "p,3dlu,p,3dlu,p,7dlu,p,3dlu,p,3dlu,p");
134: PanelBuilder builder = new PanelBuilder(layout);
135: builder.setDefaultDialogBorder();
136:
137: CellConstraints cc = new CellConstraints();
138:
139: findwhatField = new JTextField(20);
140: regexpTestButton = new JButton(this .cleverPHL.getLanguage()
141: .getString("CleverPHL.FindDialog.Test"));
142: regexpTestButton.addActionListener(this );
143: regexpCheckBox = new JCheckBox(this .cleverPHL.getLanguage()
144: .getString("CleverPHL.FindDialog.RegularExpression"));
145:
146: regexpTestButton.setEnabled(regexpCheckBox.isSelected());
147: regexpCheckBox.addActionListener(new ActionListener() {
148: public void actionPerformed(ActionEvent event) {
149: regexpTestButton
150: .setEnabled(regexpCheckBox.isSelected());
151: }
152: });
153:
154: scopeActualSessionButton = new JRadioButton(this .cleverPHL
155: .getLanguage().getString(
156: "CleverPHL.FindDialog.ScopeActualSession"));
157: scopeAllSessionsButton = new JRadioButton(this .cleverPHL
158: .getLanguage().getString(
159: "CleverPHL.FindDialog.ScopeAllSessions"));
160: scopeActualSessionButton.setSelected(true);
161:
162: ButtonGroup scopeGroup = new ButtonGroup();
163: scopeGroup.add(scopeActualSessionButton);
164: scopeGroup.add(scopeAllSessionsButton);
165:
166: builder.addSeparator(this .cleverPHL.getLanguage().getString(
167: "CleverPHL.FindDialog.Search"), cc.xyw(1, 1, 4));
168: builder.addLabel(this .cleverPHL.getLanguage().getString(
169: "CleverPHL.FindDialog.FindWhat")
170: + " :", cc.xy(2, 3));
171: builder.add(findwhatField, cc.xy(4, 3));
172: builder.add(regexpCheckBox, cc.xy(2, 5));
173: builder.add(regexpTestButton, cc.xy(4, 5));
174:
175: builder.addSeparator(this .cleverPHL.getLanguage().getString(
176: "CleverPHL.FindDialog.Scope"), cc.xyw(1, 7, 4));
177: builder.add(scopeActualSessionButton, cc.xy(2, 9));
178: builder.add(scopeAllSessionsButton, cc.xy(2, 11));
179:
180: JPanel panel = builder.getPanel();
181:
182: return panel;
183: }
184:
185: /**
186: * Called when a button has been pressed.
187: *
188: * @param event DOCUMENT ME!
189: */
190: public void actionPerformed(ActionEvent event) {
191: if (event.getSource() == regexpTestButton) {
192: testRegEx();
193: } else {
194: if (event.getSource() == okButton) {
195: okButtonPressed = true;
196: } else {
197: okButtonPressed = false;
198: }
199:
200: searchString = findwhatField.getText();
201: isRegExp = regexpCheckBox.isSelected();
202:
203: if (scopeActualSessionButton.isSelected()) {
204: scope = SCOPE_ACTUAL_SESSION;
205: } else if (scopeAllSessionsButton.isSelected()) {
206: scope = SCOPE_ALL_SESSIONS;
207: }
208:
209: dispose();
210: }
211: }
212:
213: /**
214: * Tests the regular expression.
215: */
216: private void testRegEx() {
217: try {
218: RE re = new RE(findwhatField.getText());
219: } catch (RESyntaxException res) {
220: cleverPHL
221: .getLogger()
222: .info(
223: cleverPHL
224: .getLanguage()
225: .getString(
226: "CleverPHL.FindDialog.SyntaxCheck.NotCorrect"),
227: res);
228:
229: return;
230: }
231:
232: cleverPHL.getLogger().info(
233: cleverPHL.getLanguage().getString(
234: "CleverPHL.FindDialog.SyntaxCheck.Correct"));
235: }
236:
237: /**
238: * Returns whether or not the ok button has been pressed.
239: *
240: * @return <code>true</code> if the ok button has been pressed, otherwise <code>false</code>
241: */
242: public boolean okButtonPressed() {
243: return okButtonPressed;
244: }
245:
246: /**
247: * Returns the search string.
248: *
249: * @return the string
250: */
251: public String getSearchString() {
252: return searchString;
253: }
254:
255: /**
256: * Returns whether or not the search string is a regular expression.
257: *
258: * @return <code>true</code> if the search string is a regular expression, otherwise
259: * <code>false</code>
260: */
261: public boolean isRegExp() {
262: return isRegExp;
263: }
264:
265: /**
266: * Returns the scope of the search.
267: *
268: * @return the scope.
269: */
270: public int getScope() {
271: return scope;
272: }
273: }
|