001: package org.enhydra.kelp.ant.swing;
002:
003: // AddinCore
004: import org.enhydra.kelp.common.Constants;
005: import org.enhydra.kelp.common.event.WriteEvent;
006: import org.enhydra.kelp.common.event.WriteListener;
007: import org.enhydra.kelp.common.node.OtterProject;
008: import org.enhydra.kelp.common.swing.OutputPanel;
009:
010: // ToolBox
011: import org.enhydra.tool.common.ExtensionFilter;
012: import org.enhydra.tool.common.PathHandle;
013:
014: // JDK
015: import javax.swing.*;
016: import java.awt.*;
017: import java.awt.event.*;
018: import java.beans.Beans;
019: import java.io.File;
020: import java.util.ResourceBundle;
021: import org.enhydra.kelp.ant.node.AntProject;
022:
023: //
024: public class AntOutputPanel extends JPanel implements WriteListener {
025:
026: //
027: public static ResourceBundle res = ResourceBundle
028: .getBundle("org.enhydra.kelp.common.Res"); // nores
029: private LocalButtonListener buttonListener = null;
030: private LocalCheckListener checkListener = null;
031: private OtterProject project = null;
032: private JPanel panelFile;
033: private JCheckBox checkSave;
034: private OutputPanel outputPanel;
035: private JTextField textFile;
036: private JButton buttonBrowse;
037: private GridBagLayout layoutFile;
038: private GridBagLayout layoutMain;
039:
040: /**
041: * Constructor declaration
042: *
043: */
044: public AntOutputPanel() {
045: try {
046: jbInit();
047: pmInit();
048: } catch (Exception e) {
049: e.printStackTrace();
050: }
051: }
052:
053: /**
054: * Method declaration
055: *
056: *
057: * @param event
058: */
059: public void onWrite(WriteEvent event) {
060: outputPanel.onWrite(event);
061: }
062:
063: /**
064: * Method declaration
065: *
066: *
067: * @return
068: */
069: public OtterProject getProject() {
070: return project;
071: }
072:
073: /**
074: * Method declaration
075: *
076: *
077: * @param p
078: */
079: public void setProject(OtterProject p) {
080: project = p;
081: String outPath = project.getOutputFilename();
082:
083: if ((outPath == null) || (outPath.trim().length() == 0)) {
084: checkSave.setSelected(false);
085: textFile.setText(new String());
086: } else {
087: textFile.setText(PathHandle.createPathString(outPath));
088: checkSave.setSelected(true);
089: }
090: outputPanel.setProject(project);
091: }
092:
093: public void clearOutput() {
094: outputPanel.clearOutput();
095: }
096:
097: //
098: // PROTECTED
099: //
100:
101: /**
102: * Method declaration
103: *
104: */
105: protected void scrollToBottom() {
106: outputPanel.scrollToBottom();
107: }
108:
109: //
110: // PRIVATE
111: //
112: private void buttonFileAction() {
113: JFileChooser chooser = null;
114: ExtensionFilter filter = null;
115: PathHandle path = null;
116: File file = null;
117: int choice = -1;
118:
119: //
120: filter = new ExtensionFilter();
121: filter.addExtension(Constants.TYPE_TXT);
122: filter.setDescriptionTitle(res.getString("Kelp_log"));
123:
124: //
125: chooser = new JFileChooser();
126: if (textFile.getText().trim().length() == 0) {
127: file = new File(project.getRootPath() + File.separator
128: + Constants.FILE_KELP_TXT);
129: } else {
130: file = new File(textFile.getText());
131: }
132: chooser.setSelectedFile(file);
133: chooser.setFileFilter(filter);
134: chooser.setDialogTitle(res.getString("Select_log"));
135: chooser.setApproveButtonText(res.getString("OK"));
136: choice = chooser.showOpenDialog(this );
137:
138: //
139: this .requestFocus();
140: buttonBrowse.requestFocus();
141:
142: //
143: if (choice == JFileChooser.APPROVE_OPTION
144: && chooser.getSelectedFile() != null) {
145: path = PathHandle.createPathHandle(chooser
146: .getSelectedFile());
147: textFile.setText(path.getPath());
148: project.setOutputFilename(path.getPath());
149: }
150:
151: //
152: chooser.removeAll();
153: chooser = null;
154: }
155:
156: /**
157: * Method declaration
158: *
159: */
160: private void pmInit() {
161: buttonListener = new LocalButtonListener();
162: buttonBrowse.addActionListener(buttonListener);
163: buttonBrowse.setEnabled(false);
164: checkListener = new LocalCheckListener();
165: checkSave.addItemListener(checkListener);
166: checkSave.setSelected(false);
167: }
168:
169: /**
170: * Method declaration
171: *
172: *
173: * @throws Exception
174: */
175: private void jbInit() throws Exception {
176: panelFile = (JPanel) Beans.instantiate(getClass()
177: .getClassLoader(), JPanel.class.getName());
178: outputPanel = (OutputPanel) Beans.instantiate(getClass()
179: .getClassLoader(), OutputPanel.class.getName());
180: checkSave = (JCheckBox) Beans.instantiate(getClass()
181: .getClassLoader(), JCheckBox.class.getName());
182: textFile = (JTextField) Beans.instantiate(getClass()
183: .getClassLoader(), JTextField.class.getName());
184: buttonBrowse = (JButton) Beans.instantiate(getClass()
185: .getClassLoader(), JButton.class.getName());
186: layoutFile = (GridBagLayout) Beans.instantiate(getClass()
187: .getClassLoader(), GridBagLayout.class.getName());
188: layoutMain = (GridBagLayout) Beans.instantiate(getClass()
189: .getClassLoader(), GridBagLayout.class.getName());
190: buttonBrowse.setText(res.getString("Browse"));
191: panelFile.setLayout(layoutFile);
192: checkSave.setText(res.getString("Output_to_log"));
193: textFile.setBackground(SystemColor.controlLtHighlight);
194: textFile.setEnabled(false);
195: textFile.setPreferredSize(new Dimension(150, 21));
196: panelFile
197: .add(checkSave, new GridBagConstraints(0, 0, 1, 1, 0.0,
198: 0.0, GridBagConstraints.WEST,
199: GridBagConstraints.BOTH,
200: new Insets(5, 5, 5, 5), 14, 0));
201: panelFile.add(textFile, new GridBagConstraints(1, 0, 1, 1, 0.5,
202: 0.5, GridBagConstraints.CENTER,
203: GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5),
204: 14, 0));
205: panelFile.add(buttonBrowse, new GridBagConstraints(2, 0, 1, 1,
206: 0.5, 0.5, GridBagConstraints.WEST,
207: GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 6, 0));
208: this .setLayout(layoutMain);
209: this .add(outputPanel, new GridBagConstraints(0, 0, 1, 1, 0.75,
210: 0.75, GridBagConstraints.WEST, GridBagConstraints.BOTH,
211: new Insets(8, 12, 0, 0), 0, 0));
212: this
213: .add(panelFile, new GridBagConstraints(0, 1, 1, 1,
214: 0.25, 0.25, GridBagConstraints.CENTER,
215: GridBagConstraints.BOTH,
216: new Insets(0, 12, 0, 0), 0, 0));
217: }
218:
219: /**
220: */
221: private class LocalCheckListener implements ItemListener {
222: public void itemStateChanged(ItemEvent event) {
223: Object source = event.getSource();
224:
225: if (source == checkSave) {
226: if (checkSave.isSelected()) {
227: buttonBrowse.setEnabled(true);
228: ((AntProject) project).setOutputFileEnabled(true);
229: } else {
230: buttonBrowse.setEnabled(false);
231: textFile.setText(new String());
232: if (project == null) {
233:
234: // done
235: } else {
236: project.setOutputFilename(new String());
237: ((AntProject) project)
238: .setOutputFileEnabled(false);
239: }
240: }
241: }
242: }
243:
244: }
245:
246: /**
247: */
248: private class LocalButtonListener implements ActionListener {
249: public void actionPerformed(ActionEvent event) {
250: Object source = event.getSource();
251:
252: if (source == buttonBrowse) {
253: buttonFileAction();
254: }
255: }
256:
257: }
258:
259: }
|