PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/tools/java/MainWindow.java

https://bitbucket.org/val_haris/asc-ai
Java | 385 lines | 229 code | 71 blank | 85 comment | 40 complexity | 8645739ad9145f06515ad572c868f588 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. // $Id: MainWindow.java,v 1.11 2009-04-18 13:48:40 mbickel Exp $
  2. //
  3. // $Log: not supported by cvs2svn $
  4. // Revision 1.10 2000/11/08 19:50:25 mbickel
  5. // Fixed Nullpointer exception when the last directory doesn't exist any
  6. // more on startup.
  7. //
  8. // Revision 1.9 2000/11/07 16:19:39 schelli
  9. // Minor Memory-Functions & Problems changed
  10. // Picture Support partly enabled
  11. // New Funktion partly implemented
  12. // Buildings partly implemented
  13. //
  14. // Revision 1.8 2000/11/01 11:41:04 mbickel
  15. // Fixed: Selection in MainWindow-List not being evaluated, the first list
  16. // entry was always edited.
  17. //
  18. // Revision 1.7 2000/10/31 18:06:46 mbickel
  19. // Fileselector now displays files too
  20. //
  21. // Revision 1.6 2000/10/29 21:06:03 mbickel
  22. // Modified filename construction to run under Linux
  23. // Started implementing a better directory choosing dialog
  24. //
  25. // Revision 1.5 2000/10/24 15:35:13 schelli
  26. // MapEd FullScreen support added
  27. // weapons ammo now editable in MapEd
  28. //
  29. // Revision 1.4 2000/10/17 20:21:03 schelli
  30. // App now remembers path
  31. //
  32. // Revision 1.3 2000/10/17 17:28:26 schelli
  33. // minor bugs fixed in lots of sources
  34. // add & remove weapon works now
  35. // revert to save button removed
  36. // class-handling bugs fixed
  37. // load & save routines fully implemented
  38. // terrainacces added
  39. //
  40. // Revision 1.2 2000/10/13 13:15:46 schelli
  41. // Load&Save routines finished
  42. //
  43. /*
  44. * MainWindow.java
  45. *
  46. * Created on 7. November 1999, 01:36
  47. This file is part of Advanced Strategic Command; http://www.asc-hq.de
  48. Copyright (C) 1994-2000 Martin Bickel and Marc Schellenberger
  49. This program is free software; you can redistribute it and/or modify
  50. it under the terms of the GNU General Public License as published by
  51. the Free Software Foundation; either version 2 of the License, or
  52. (at your option) any later version.
  53. This program is distributed in the hope that it will be useful,
  54. but WITHOUT ANY WARRANTY; without even the implied warranty of
  55. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  56. GNU General Public License for more details.
  57. You should have received a copy of the GNU General Public License
  58. along with this program; see the file COPYING. If not, write to the
  59. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  60. Boston, MA 02111-1307 USA
  61. */
  62. public class MainWindow extends javax.swing.JFrame {
  63. int selection = 0;
  64. int initDone;
  65. public javax.swing.JList fileList;
  66. ErrorMessage errMsg;
  67. SgStream input;
  68. SgStream output;
  69. String CfgFile = "MainWindow.cfg";
  70. String paths[];
  71. String files[];
  72. /** Creates new form JFrame */
  73. public MainWindow() {
  74. initComponents ();
  75. initDone = 0;
  76. setTitle("ASC File Editor");
  77. for(int i= 0; i < descriptions.length;i++)
  78. jComboBoxType.addItem(descriptions[i]);
  79. paths = new String[descriptions.length];
  80. for(int i= 0; i < descriptions.length;i++) paths[i] =".";
  81. input = new SgStream(CfgFile,input.STREAM_READ);
  82. if (input.error != 1) {
  83. int num = input.readChar();
  84. if ( num > descriptions.length ) num = descriptions.length;
  85. for(int i= 0; i < num;i++)
  86. paths[i] = input.readPChar();
  87. }
  88. fileList = new javax.swing.JList();
  89. fileList.setSelectionMode(new javax.swing.DefaultListSelectionModel().SINGLE_SELECTION);
  90. jScrollPaneFiles.getViewport().setView(fileList);
  91. errMsg = new ErrorMessage(jLabelErrorMessage);
  92. selFiles();
  93. jTextFieldPath.setText (paths[selection]);
  94. initDone = 1;
  95. pack ();
  96. }
  97. /** This method is called from within the constructor to
  98. * initialize the form.
  99. * WARNING: Do NOT modify this code. The content of this method is
  100. * always regenerated by the FormEditor.
  101. */
  102. private void initComponents () {//GEN-BEGIN:initComponents
  103. jLabelErrorMessage = new javax.swing.JLabel ();
  104. jTextFieldPath = new javax.swing.JTextField ();
  105. jButtonEdit = new javax.swing.JButton ();
  106. jButtonBrowsePath = new javax.swing.JButton ();
  107. jScrollPaneFiles = new javax.swing.JScrollPane ();
  108. jButtonNew = new javax.swing.JButton ();
  109. jComboBoxType = new javax.swing.JComboBox ();
  110. getContentPane ().setLayout (new org.netbeans.lib.awtextra.AbsoluteLayout ());
  111. addWindowListener (new java.awt.event.WindowAdapter () {
  112. public void windowClosing (java.awt.event.WindowEvent evt) {
  113. exitForm (evt);
  114. }
  115. }
  116. );
  117. jLabelErrorMessage.setBorder (new javax.swing.border.EtchedBorder(java.awt.Color.white, new java.awt.Color (134, 134, 134)));
  118. getContentPane ().add (jLabelErrorMessage, new org.netbeans.lib.awtextra.AbsoluteConstraints (0, 370, 530, 40));
  119. jTextFieldPath.setBorder (new javax.swing.border.TitledBorder(
  120. new javax.swing.border.EtchedBorder(java.awt.Color.white, new java.awt.Color (134, 134, 134)),
  121. "Path", 4, 2, new java.awt.Font ("Arial", 0, 10), java.awt.Color.black));
  122. jTextFieldPath.setText (".");
  123. jTextFieldPath.addActionListener (new java.awt.event.ActionListener () {
  124. public void actionPerformed (java.awt.event.ActionEvent evt) {
  125. jTextFieldPathActionPerformed (evt);
  126. }
  127. }
  128. );
  129. jTextFieldPath.addFocusListener (new java.awt.event.FocusAdapter () {
  130. public void focusLost (java.awt.event.FocusEvent evt) {
  131. jTextFieldPathFocusLost (evt);
  132. }
  133. }
  134. );
  135. getContentPane ().add (jTextFieldPath, new org.netbeans.lib.awtextra.AbsoluteConstraints (10, 310, 350, 50));
  136. jButtonEdit.setActionCommand ("Edit");
  137. jButtonEdit.setText ("Edit");
  138. jButtonEdit.addActionListener (new java.awt.event.ActionListener () {
  139. public void actionPerformed (java.awt.event.ActionEvent evt) {
  140. jButtonEditActionPerformed (evt);
  141. }
  142. }
  143. );
  144. getContentPane ().add (jButtonEdit, new org.netbeans.lib.awtextra.AbsoluteConstraints (380, 200, 130, 60));
  145. jButtonBrowsePath.setText ("Browse");
  146. jButtonBrowsePath.addActionListener (new java.awt.event.ActionListener () {
  147. public void actionPerformed (java.awt.event.ActionEvent evt) {
  148. jButtonBrowsePathActionPerformed (evt);
  149. }
  150. }
  151. );
  152. getContentPane ().add (jButtonBrowsePath, new org.netbeans.lib.awtextra.AbsoluteConstraints (380, 320, 130, 30));
  153. getContentPane ().add (jScrollPaneFiles, new org.netbeans.lib.awtextra.AbsoluteConstraints (10, 70, 350, 230));
  154. jButtonNew.setActionCommand ("New");
  155. jButtonNew.setText ("New");
  156. jButtonNew.addActionListener (new java.awt.event.ActionListener () {
  157. public void actionPerformed (java.awt.event.ActionEvent evt) {
  158. jButtonNewActionPerformed (evt);
  159. }
  160. }
  161. );
  162. getContentPane ().add (jButtonNew, new org.netbeans.lib.awtextra.AbsoluteConstraints (380, 100, 130, 60));
  163. jComboBoxType.addItemListener (new java.awt.event.ItemListener () {
  164. public void itemStateChanged (java.awt.event.ItemEvent evt) {
  165. jComboBoxTypeItemStateChanged (evt);
  166. }
  167. }
  168. );
  169. getContentPane ().add (jComboBoxType, new org.netbeans.lib.awtextra.AbsoluteConstraints (10, 10, 350, 50));
  170. }//GEN-END:initComponents
  171. private void jButtonNewActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNewActionPerformed
  172. // Add your handling code here:
  173. switch (selection) {
  174. case 0 : new UnitEditor (paths[selection],"new.veh",tUnit.NEW,new UnitEditor("","",0,0).DISPOSE).show ();
  175. default : ;
  176. }
  177. }//GEN-LAST:event_jButtonNewActionPerformed
  178. private void jComboBoxTypeItemStateChanged (java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxTypeItemStateChanged
  179. // Add your handling code here:
  180. if ( initDone == 1) {
  181. selection = jComboBoxType.getSelectedIndex();
  182. jTextFieldPath.setText (paths[selection]);
  183. selFiles();
  184. }
  185. }//GEN-LAST:event_jComboBoxTypeItemStateChanged
  186. private void jTextFieldPathFocusLost (java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldPathFocusLost
  187. // Add your handling code here:
  188. pathAction();
  189. }//GEN-LAST:event_jTextFieldPathFocusLost
  190. private void jTextFieldPathActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldPathActionPerformed
  191. // Add your handling code here:
  192. pathAction();
  193. }//GEN-LAST:event_jTextFieldPathActionPerformed
  194. private void jButtonEditActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEditActionPerformed
  195. // Add your handling code here:
  196. int sel = fileList.getSelectedIndex();
  197. if (sel >= 0) {
  198. String selName = files[sel];
  199. switch (selection) {
  200. case 0 : new UnitEditor (paths[selection],selName,tUnit.LOAD,new UnitEditor("","",0,0).DISPOSE).show ();
  201. default : ;
  202. }
  203. }
  204. }//GEN-LAST:event_jButtonEditActionPerformed
  205. private void jButtonBrowsePathActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBrowsePathActionPerformed
  206. // Add your handling code here:
  207. String dir;
  208. if ( (dir = browsePath(paths[selection])) != null ) {
  209. int i = dir.lastIndexOf(java.io.File.separator);
  210. if(i>0 && i<dir.length()-1) {
  211. paths[selection] = dir.substring(0, i);
  212. //paths[selection] = dir;
  213. jTextFieldPath.setText (paths[selection]);
  214. selFiles();
  215. fileList.setSelectedValue(dir.substring(i+1), true);
  216. };
  217. }
  218. }//GEN-LAST:event_jButtonBrowsePathActionPerformed
  219. /** Exit the Application */
  220. private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
  221. output = new SgStream(CfgFile,output.STREAM_WRITE);
  222. if (output.error != 1) {
  223. output.writeChar(descriptions.length);
  224. for(int i= 0; i < descriptions.length;i++)
  225. output.writePChar(paths[i]);
  226. }
  227. System.exit (0);
  228. }//GEN-LAST:event_exitForm
  229. private void pathAction() {
  230. String path;
  231. path = jTextFieldPath.getText ();
  232. if ( checkPath(path) != 0 ) {
  233. jTextFieldPath.setText (paths[selection]);
  234. errMsg.showMessage("Invalid Path !");
  235. } else {
  236. paths[selection] = path;
  237. selFiles();
  238. }
  239. }
  240. private String browsePath(String startPath) {
  241. javax.swing.JFileChooser jFileCh;
  242. ExampleFileFilter filter = new ExampleFileFilter( extensions ); //extensions
  243. jFileCh = new javax.swing.JFileChooser(startPath);
  244. jFileCh.setFileFilter ( filter );
  245. //jFileCh.setFileSelectionMode(jFileCh.DIRECTORIES_ONLY);
  246. if ( jFileCh.showOpenDialog(jButtonBrowsePath) == jFileCh.APPROVE_OPTION ) {
  247. return jFileCh.getSelectedFile().getAbsolutePath();
  248. } else return null;
  249. }
  250. private void selFiles() {
  251. String allFiles[];
  252. String ext;
  253. java.io.File f;
  254. int i = 0;
  255. f = new java.io.File(paths[selection]);
  256. if ( !f.isDirectory() ) {
  257. paths[selection] = ".";
  258. f = new java.io.File(paths[selection]);
  259. }
  260. allFiles = f.list();
  261. for (int j = 0; j < allFiles.length; j ++) {
  262. ext = getExtension(allFiles[j]);
  263. if (ext != null)
  264. if (ext.compareTo(extensions[selection]) == 0) {
  265. i++;
  266. }
  267. }
  268. files = new String[i];
  269. i=0;
  270. for (int j = 0; j < allFiles.length; j ++) {
  271. ext = getExtension(allFiles[j]);
  272. if (ext != null)
  273. if (ext.compareTo(extensions[selection]) == 0) {
  274. files[i] = allFiles[j];
  275. i++;
  276. }
  277. }
  278. fileList.setListData(files);
  279. }
  280. public String getExtension(String filename) {
  281. int i = filename.lastIndexOf('.');
  282. if(i>0 && i<filename.length()-1) {
  283. return filename.substring(i+1).toLowerCase();
  284. };
  285. return null;
  286. }
  287. private int checkPath(String path) {
  288. java.io.File cFile = new java.io.File(path);
  289. if (cFile.isDirectory() == true) return 0;
  290. else return 1;
  291. }
  292. /**
  293. * @param args the command line arguments
  294. */
  295. public static void main (String args[]) {
  296. new MainWindow ().show ();
  297. }
  298. // Variables declaration - do not modify//GEN-BEGIN:variables
  299. private javax.swing.JLabel jLabelErrorMessage;
  300. private javax.swing.JTextField jTextFieldPath;
  301. private javax.swing.JButton jButtonEdit;
  302. private javax.swing.JButton jButtonBrowsePath;
  303. private javax.swing.JScrollPane jScrollPaneFiles;
  304. private javax.swing.JButton jButtonNew;
  305. private javax.swing.JComboBox jComboBoxType;
  306. // End of variables declaration//GEN-END:variables
  307. public Unit tUnit;
  308. static String[] descriptions = {"Vehicles","Buildings","Terrain","Technologies","Icons"};
  309. static String[] extensions = {"veh","bld","trr","tec","nic"};
  310. }