1 23 package org.enhydra.kelp.common.xmlc; 24 25 import org.enhydra.kelp.common.Constants; 27 import org.enhydra.kelp.common.event.WriteEvent; 28 import org.enhydra.kelp.common.event.WriteListener; 29 import org.enhydra.kelp.common.node.OtterProject; 30 import org.enhydra.kelp.common.swing.OutputPanel; 31 32 import org.enhydra.tool.common.ExtensionFilter; 34 import org.enhydra.tool.common.PathHandle; 35 36 import javax.swing.*; 38 import javax.swing.text.BadLocationException ; 39 import java.awt.*; 40 import java.awt.event.*; 41 import java.beans.Beans ; 42 import java.io.File ; 43 import java.io.FileWriter ; 44 import java.util.ResourceBundle ; 45 46 public class XMLCOutputPanel extends JPanel implements WriteListener { 48 49 public static ResourceBundle res = 51 ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); private LocalButtonListener buttonListener = null; 53 private LocalCheckListener checkListener = null; 54 private OtterProject project = null; 55 private JPanel panelFile; 56 private JCheckBox checkSave; 57 private OutputPanel outputPanel; 58 private JTextField textFile; 59 private JButton buttonBrowse; 60 private GridBagLayout layoutFile; 61 private GridBagLayout layoutMain; 62 private int waitCount = 0; 63 64 68 public XMLCOutputPanel() { 69 try { 70 jbInit(); 71 pmInit(); 72 } catch (Exception e) { 73 e.printStackTrace(); 74 } 75 } 76 77 83 public void onWrite(WriteEvent event) { 84 outputPanel.onWrite(event); 85 } 86 87 93 public OtterProject getProject() { 94 return project; 95 } 96 97 103 public void setProject(OtterProject p) { 104 project = p; 105 String outPath = project.getOutputFilename(); 106 107 if ((outPath == null) || (outPath.trim().length() == 0)) { 108 checkSave.setSelected(false); 109 textFile.setText(new String ()); 110 } else { 111 textFile.setText(PathHandle.createPathString(outPath)); 112 checkSave.setSelected(true); 113 } 114 outputPanel.setProject(project); 115 } 116 117 public void clearOutput() { 118 outputPanel.clearOutput(); 119 } 120 121 125 129 protected void scrollToBottom() { 130 outputPanel.scrollToBottom(); 131 } 132 133 private void buttonFileAction() { 137 JFileChooser chooser = null; 138 ExtensionFilter filter = null; 139 PathHandle path = null; 140 File file = null; 141 String fileDir = new String (); 142 int choice = -1; 143 144 filter = new ExtensionFilter(); 146 filter.addExtension(Constants.TYPE_TXT); 147 filter.setDescriptionTitle(res.getString("Kelp_log")); 148 149 chooser = new JFileChooser(); 151 if (textFile.getText().trim().length() == 0) { 152 file = new File (project.getRootPath() + File.separator 153 + Constants.FILE_KELP_TXT); 154 } else { 155 file = new File (textFile.getText()); 156 } 157 chooser.setSelectedFile(file); 158 chooser.setFileFilter(filter); 159 chooser.setDialogTitle(res.getString("Select_log")); 160 chooser.setApproveButtonText(res.getString("OK")); 161 choice = chooser.showOpenDialog(this); 162 163 this.requestFocus(); 165 buttonBrowse.requestFocus(); 166 167 if (choice == JFileChooser.APPROVE_OPTION 169 && chooser.getSelectedFile() != null) { 170 path = PathHandle.createPathHandle(chooser.getSelectedFile()); 171 textFile.setText(path.getPath()); 172 project.setOutputFilename(path.getPath()); 173 } 174 175 chooser.removeAll(); 177 chooser = null; 178 } 179 180 184 private void pmInit() { 185 buttonListener = new LocalButtonListener(); 186 buttonBrowse.addActionListener(buttonListener); 187 buttonBrowse.setEnabled(false); 188 checkListener = new LocalCheckListener(); 189 checkSave.addItemListener(checkListener); 190 checkSave.setSelected(false); 191 } 192 193 199 private void jbInit() throws Exception { 200 panelFile = (JPanel) Beans.instantiate(getClass().getClassLoader(), 201 JPanel.class.getName()); 202 outputPanel = 203 (OutputPanel) Beans.instantiate(getClass().getClassLoader(), 204 OutputPanel.class.getName()); 205 checkSave = (JCheckBox) Beans.instantiate(getClass().getClassLoader(), 206 JCheckBox.class.getName()); 207 textFile = (JTextField) Beans.instantiate(getClass().getClassLoader(), 208 JTextField.class.getName()); 209 buttonBrowse = 210 (JButton) Beans.instantiate(getClass().getClassLoader(), 211 JButton.class.getName()); 212 layoutFile = 213 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 214 GridBagLayout.class.getName()); 215 layoutMain = 216 (GridBagLayout) Beans.instantiate(getClass().getClassLoader(), 217 GridBagLayout.class.getName()); 218 buttonBrowse.setText(res.getString("Browse")); 219 panelFile.setLayout(layoutFile); 220 checkSave.setText(res.getString("Output_to_log")); 221 textFile.setBackground(SystemColor.controlLtHighlight); 222 textFile.setEnabled(false); 223 textFile.setPreferredSize(new Dimension(150, 21)); 224 panelFile.add(checkSave, 225 new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, 226 GridBagConstraints.WEST, 227 GridBagConstraints.BOTH, 228 new Insets(5, 5, 5, 5), 14, 0)); 229 panelFile.add(textFile, 230 new GridBagConstraints(1, 0, 1, 1, 0.5, 0.5, 231 GridBagConstraints.CENTER, 232 GridBagConstraints.HORIZONTAL, 233 new Insets(5, 5, 5, 5), 14, 0)); 234 panelFile.add(buttonBrowse, 235 new GridBagConstraints(2, 0, 1, 1, 0.5, 0.5, 236 GridBagConstraints.WEST, 237 GridBagConstraints.NONE, 238 new Insets(5, 5, 5, 5), 6, 0)); 239 this.setLayout(layoutMain); 240 this.add(outputPanel, 241 new GridBagConstraints(0, 0, 1, 1, 0.75, 0.75, 242 GridBagConstraints.WEST, 243 GridBagConstraints.BOTH, 244 new Insets(8, 12, 0, 0), 0, 0)); 245 this.add(panelFile, 246 new GridBagConstraints(0, 1, 1, 1, 0.25, 0.25, 247 GridBagConstraints.CENTER, 248 GridBagConstraints.BOTH, 249 new Insets(0, 12, 0, 0), 0, 0)); 250 } 251 252 254 private class LocalCheckListener implements ItemListener { 255 public void itemStateChanged(ItemEvent event) { 256 Object source = event.getSource(); 257 258 if (source == checkSave) { 259 if (checkSave.isSelected()) { 260 buttonBrowse.setEnabled(true); 261 } else { 262 buttonBrowse.setEnabled(false); 263 textFile.setText(new String ()); 264 if (project == null) { 265 266 } else { 268 project.setOutputFilename(new String ()); 269 } 270 } 271 } 272 } 273 274 } 275 276 278 private class LocalButtonListener implements ActionListener { 279 public void actionPerformed(ActionEvent event) { 280 Object source = event.getSource(); 281 282 if (source == buttonBrowse) { 283 buttonFileAction(); 284 } 285 } 286 287 } 288 } 289 | Popular Tags |