1*cd519653SAndrew Rist /**************************************************************
2*cd519653SAndrew Rist  *
3*cd519653SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*cd519653SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*cd519653SAndrew Rist  * distributed with this work for additional information
6*cd519653SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*cd519653SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*cd519653SAndrew Rist  * "License"); you may not use this file except in compliance
9*cd519653SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*cd519653SAndrew Rist  *
11*cd519653SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*cd519653SAndrew Rist  *
13*cd519653SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*cd519653SAndrew Rist  * software distributed under the License is distributed on an
15*cd519653SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*cd519653SAndrew Rist  * KIND, either express or implied.  See the License for the
17*cd519653SAndrew Rist  * specific language governing permissions and limitations
18*cd519653SAndrew Rist  * under the License.
19*cd519653SAndrew Rist  *
20*cd519653SAndrew Rist  *************************************************************/
21*cd519653SAndrew Rist 
22cdf0e10cSrcweir package installer;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir /*
25cdf0e10cSrcweir  * Welcome.java
26cdf0e10cSrcweir  *
27cdf0e10cSrcweir  * Created on 04 July 2002, 15:43
28cdf0e10cSrcweir  */
29cdf0e10cSrcweir 
30cdf0e10cSrcweir /**
31cdf0e10cSrcweir  *
32cdf0e10cSrcweir  * @author  mike
33cdf0e10cSrcweir  */
34cdf0e10cSrcweir 
35cdf0e10cSrcweir import java.awt.*;
36cdf0e10cSrcweir import java.awt.event.*;
37cdf0e10cSrcweir import java.io.*;
38cdf0e10cSrcweir import java.util.*;
39cdf0e10cSrcweir import javax.swing.*;
40cdf0e10cSrcweir import javax.swing.event.*;
41cdf0e10cSrcweir import javax.swing.table.*;
42cdf0e10cSrcweir import javax.swing.SwingUtilities.*;
43cdf0e10cSrcweir 
44cdf0e10cSrcweir public class IdeVersion extends javax.swing.JPanel implements ActionListener, TableModelListener {
45cdf0e10cSrcweir 
46cdf0e10cSrcweir     /** Creates new form Welcome */
IdeVersion(InstallWizard wizard)47cdf0e10cSrcweir     public IdeVersion(InstallWizard wizard) {
48cdf0e10cSrcweir         this.wizard=wizard;
49cdf0e10cSrcweir 	setBackground(Color.white);
50cdf0e10cSrcweir         initComponents();
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir 
53cdf0e10cSrcweir     /** This method is called from within the constructor to
54cdf0e10cSrcweir      * initialize the form.
55cdf0e10cSrcweir      * WARNING: Do NOT modify this code. The content of this method is
56cdf0e10cSrcweir      * always regenerated by the Form Editor.
57cdf0e10cSrcweir      */
initComponents()58cdf0e10cSrcweir     private void initComponents() {
59cdf0e10cSrcweir         Properties props = null;
60cdf0e10cSrcweir         JPanel versionPanel = new JPanel();
61cdf0e10cSrcweir         setLayout(new BorderLayout());
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	try {
65cdf0e10cSrcweir         	//props = InstUtil.getNetbeansLocation();
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 		Properties netbeansProps = InstUtil.getNetbeansLocation();
68cdf0e10cSrcweir 		//Properties jeditProps = InstUtil.getJeditLocation();
69cdf0e10cSrcweir 		Properties ideProps = new Properties();
70cdf0e10cSrcweir 		if(netbeansProps!=null )
71cdf0e10cSrcweir 		{
72cdf0e10cSrcweir                 System.out.println("**** Found netbeans install");
73cdf0e10cSrcweir 		for( int n = 0; n < netbeansProps.size(); n++ ) {
74cdf0e10cSrcweir 			for( int v = 0; v < InstUtil.versions.length; v++ ) {
75cdf0e10cSrcweir 				System.out.println("n: " +n+" v: " +v);
76cdf0e10cSrcweir 				String key = InstUtil.versions[v];
77cdf0e10cSrcweir 				System.out.println("It got here1");
78cdf0e10cSrcweir 				String path = null;
79cdf0e10cSrcweir 				if ( (path = netbeansProps.getProperty(key) ) != null ) {
80cdf0e10cSrcweir 					//System.out.println( "n="+n+" v="+v + " Netbeans " + " key=" + key + " path=" + path );
81cdf0e10cSrcweir 					ideProps.put(key, path);
82cdf0e10cSrcweir 				}
83cdf0e10cSrcweir 			}
84cdf0e10cSrcweir 		}
85cdf0e10cSrcweir 		}
86cdf0e10cSrcweir                 //System.out.println("*** About to look for jedit install");
87cdf0e10cSrcweir 		/*
88cdf0e10cSrcweir 		if(jeditProps!=null)
89cdf0e10cSrcweir 		{
90cdf0e10cSrcweir 			for( int j = 0; j < jeditProps.size(); j++ ) {
91cdf0e10cSrcweir 				for( int v = 0; v < InstUtil.versions.length; v++ ) {
92cdf0e10cSrcweir 				System.out.println("j: " +j+" v: " +v);
93cdf0e10cSrcweir 					String key = InstUtil.versions[v];
94cdf0e10cSrcweir 					String path = null;
95cdf0e10cSrcweir 					if ((path = jeditProps.getProperty(key)) != null) {
96cdf0e10cSrcweir 						//System.out.println( "j="+j+" v="+v + " jEdit " + " key=" + key + " path=" + path );
97cdf0e10cSrcweir 						ideProps.put(key, path);
98cdf0e10cSrcweir 					}
99cdf0e10cSrcweir 				}
100cdf0e10cSrcweir 			}
101cdf0e10cSrcweir 		}
102cdf0e10cSrcweir 		*/
103cdf0e10cSrcweir 		props = ideProps;
104cdf0e10cSrcweir 	}
105cdf0e10cSrcweir         catch (IOException eIO) {
106cdf0e10cSrcweir             System.err.println("Failed to parse .netbeans/ide.log");
107cdf0e10cSrcweir 	    //JOptionPane.showMessageDialog(this, "There was a problem reading from the NetBeans ide.log file.", "Parse Error", JOptionPane.ERROR_MESSAGE);
108cdf0e10cSrcweir         }
109cdf0e10cSrcweir         catch (Exception e) {
110cdf0e10cSrcweir             System.err.println("Exception thrown in initComponents");
111cdf0e10cSrcweir         }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir 	tableModel = new MyTableModelIDE (props, InstUtil.versions);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	if (tableModel.getRowCount() == 0)
116cdf0e10cSrcweir 	{
117cdf0e10cSrcweir             JOptionPane.showMessageDialog(this, "No compatible IDEs were found.", "Invalid versions", JOptionPane.ERROR_MESSAGE);
118cdf0e10cSrcweir             //wizard.exitForm(null);
119cdf0e10cSrcweir 	}
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         tableModel.addTableModelListener(this);
122cdf0e10cSrcweir         JTable tableVersions = new JTable(tableModel) {
123cdf0e10cSrcweir             public String getToolTipText(MouseEvent event)
124cdf0e10cSrcweir             {
125cdf0e10cSrcweir                 int col = columnAtPoint( event.getPoint() );
126cdf0e10cSrcweir                 if (col != 2)
127cdf0e10cSrcweir                     return null;
128cdf0e10cSrcweir 
129cdf0e10cSrcweir                 int row = rowAtPoint( event.getPoint() );
130cdf0e10cSrcweir                 Object o = getValueAt(row, col);
131cdf0e10cSrcweir 
132cdf0e10cSrcweir                 if (o == null)
133cdf0e10cSrcweir                     return null;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir                 if (o.toString().equals(""))
136cdf0e10cSrcweir                     return null;
137cdf0e10cSrcweir 
138cdf0e10cSrcweir                 return o.toString();
139cdf0e10cSrcweir             }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             public Point getToolTipLocation(MouseEvent event)
142cdf0e10cSrcweir             {
143cdf0e10cSrcweir                 int col = columnAtPoint( event.getPoint() );
144cdf0e10cSrcweir                 if (col != 2)
145cdf0e10cSrcweir                     return null;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir                 int row = rowAtPoint( event.getPoint() );
148cdf0e10cSrcweir                 Object o = getValueAt(row,col);
149cdf0e10cSrcweir 
150cdf0e10cSrcweir                 if (o == null)
151cdf0e10cSrcweir                     return null;
152cdf0e10cSrcweir 
153cdf0e10cSrcweir                 if (o.toString().equals(""))
154cdf0e10cSrcweir                     return null;
155cdf0e10cSrcweir 
156cdf0e10cSrcweir                 Point pt = getCellRect(row, col, true).getLocation();
157cdf0e10cSrcweir                 pt.translate(-1,-2);
158cdf0e10cSrcweir                 return pt;
159cdf0e10cSrcweir             }
160cdf0e10cSrcweir         };
161cdf0e10cSrcweir 
162cdf0e10cSrcweir         JScrollPane scroll = new JScrollPane(tableVersions);
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         tableVersions.setPreferredSize(
165cdf0e10cSrcweir             new Dimension(InstallWizard.DEFWIDTH,InstallWizard.DEFHEIGHT));
166cdf0e10cSrcweir 
167cdf0e10cSrcweir         tableVersions.setRowSelectionAllowed(false);
168cdf0e10cSrcweir         tableVersions.setColumnSelectionAllowed(false);
169cdf0e10cSrcweir         tableVersions.setCellSelectionEnabled(false);
170cdf0e10cSrcweir 
171cdf0e10cSrcweir         initColumnSizes(tableVersions, tableModel);
172cdf0e10cSrcweir         versionPanel.add(scroll);
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         JTextArea area = new JTextArea("Please select IDEs below that you wish to add Scripting support to");
175cdf0e10cSrcweir         area.setLineWrap(true);
176cdf0e10cSrcweir         area.setEditable(false);
177cdf0e10cSrcweir         add(area, BorderLayout.NORTH);
178cdf0e10cSrcweir         add(versionPanel, BorderLayout.CENTER);
179cdf0e10cSrcweir         nav = new NavPanel(wizard, true, false, true, InstallWizard.IDEWELCOME, InstallWizard.IDEFINAL);
180cdf0e10cSrcweir         nav.setNextListener(this);
181cdf0e10cSrcweir         add(nav, BorderLayout.SOUTH);
182cdf0e10cSrcweir 
183cdf0e10cSrcweir     }// initComponents
184cdf0e10cSrcweir 
185cdf0e10cSrcweir 
getPreferredSize()186cdf0e10cSrcweir     public java.awt.Dimension getPreferredSize() {
187cdf0e10cSrcweir         return new java.awt.Dimension(320, 280);
188cdf0e10cSrcweir     }
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 
actionPerformed(ActionEvent ev)191cdf0e10cSrcweir     public void actionPerformed(ActionEvent ev) {
192cdf0e10cSrcweir         wizard.clearLocations();
193cdf0e10cSrcweir         int len = tableModel.data.size();
194cdf0e10cSrcweir         for (int i = 0; i < len; i++) {
195cdf0e10cSrcweir             ArrayList list = (ArrayList)tableModel.data.get(i);
196cdf0e10cSrcweir             if (((Boolean)list.get(0)).booleanValue() == true)
197cdf0e10cSrcweir                 wizard.storeLocation((String)list.get(2));
198cdf0e10cSrcweir         }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         //System.out.println(wizard.getLocations());
201cdf0e10cSrcweir     }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir 
tableChanged(TableModelEvent e)204cdf0e10cSrcweir     public void tableChanged(TableModelEvent e) {
205cdf0e10cSrcweir         if (tableModel.isAnySelected()) {
206cdf0e10cSrcweir             nav.enableNext(true);
207cdf0e10cSrcweir         }
208cdf0e10cSrcweir         else {
209cdf0e10cSrcweir             nav.enableNext(false);
210cdf0e10cSrcweir         }
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir 
initColumnSizes(JTable table, MyTableModelIDE model)213cdf0e10cSrcweir     private void initColumnSizes(JTable table, MyTableModelIDE model) {
214cdf0e10cSrcweir         TableColumn column = null;
215cdf0e10cSrcweir         Component comp = null;
216cdf0e10cSrcweir         int headerWidth = 0;
217cdf0e10cSrcweir         int cellWidth = 0;
218cdf0e10cSrcweir         int preferredWidth = 0;
219cdf0e10cSrcweir         int totalWidth = 0;
220cdf0e10cSrcweir         Object[] longValues = model.longValues;
221cdf0e10cSrcweir 
222cdf0e10cSrcweir         for (int i = 0; i < 3; i++) {
223cdf0e10cSrcweir             column = table.getColumnModel().getColumn(i);
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             try {
226cdf0e10cSrcweir                 comp = column.getHeaderRenderer().
227cdf0e10cSrcweir                              getTableCellRendererComponent(
228cdf0e10cSrcweir                                  null, column.getHeaderValue(),
229cdf0e10cSrcweir                                  false, false, 0, 0);
230cdf0e10cSrcweir                 headerWidth = comp.getPreferredSize().width;
231cdf0e10cSrcweir             } catch (NullPointerException e) {
232cdf0e10cSrcweir                 // System.err.println("Null pointer exception!");
233cdf0e10cSrcweir                 // System.err.println("  getHeaderRenderer returns null in 1.3.");
234cdf0e10cSrcweir                 // System.err.println("  The replacement is getDefaultRenderer.");
235cdf0e10cSrcweir             }
236cdf0e10cSrcweir 
237cdf0e10cSrcweir             // need to replace spaces in String before getting preferred width
238cdf0e10cSrcweir             if (longValues[i] instanceof String) {
239cdf0e10cSrcweir                 longValues[i] = ((String)longValues[i]).replace(' ', '_');
240cdf0e10cSrcweir             }
241cdf0e10cSrcweir 
242cdf0e10cSrcweir             System.out.println("longValues: " + longValues[i]);
243cdf0e10cSrcweir             comp = table.getDefaultRenderer(model.getColumnClass(i)).
244cdf0e10cSrcweir                          getTableCellRendererComponent(
245cdf0e10cSrcweir                              table, longValues[i],
246cdf0e10cSrcweir                              false, false, 0, i);
247cdf0e10cSrcweir             cellWidth = comp.getPreferredSize().width;
248cdf0e10cSrcweir 
249cdf0e10cSrcweir             preferredWidth = Math.max(headerWidth, cellWidth);
250cdf0e10cSrcweir 
251cdf0e10cSrcweir             if (false) {
252cdf0e10cSrcweir                 System.out.println("Initializing width of column "
253cdf0e10cSrcweir                     + i + ". "
254cdf0e10cSrcweir                     + "preferredWidth = " + preferredWidth
255cdf0e10cSrcweir                     + "; totalWidth = " + totalWidth
256cdf0e10cSrcweir                     + "; leftWidth = " + (InstallWizard.DEFWIDTH - totalWidth));
257cdf0e10cSrcweir             }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir             //XXX: Before Swing 1.1 Beta 2, use setMinWidth instead.
260cdf0e10cSrcweir             if (i == 2) {
261cdf0e10cSrcweir                 if (preferredWidth > InstallWizard.DEFWIDTH - totalWidth)
262cdf0e10cSrcweir                     column.setPreferredWidth(InstallWizard.DEFWIDTH - totalWidth);
263cdf0e10cSrcweir                 else
264cdf0e10cSrcweir                     column.setPreferredWidth(preferredWidth);
265cdf0e10cSrcweir             }
266cdf0e10cSrcweir             else {
267cdf0e10cSrcweir                 column.setMinWidth(preferredWidth);
268cdf0e10cSrcweir                 totalWidth += preferredWidth;
269cdf0e10cSrcweir             }
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir 
273cdf0e10cSrcweir     // Variables declaration - do not modify//GEN-BEGIN:variables
274cdf0e10cSrcweir     private javax.swing.JTextField jTextField2;
275cdf0e10cSrcweir 	private InstallWizard wizard;
276cdf0e10cSrcweir 	private MyTableModelIDE  tableModel;
277cdf0e10cSrcweir 	private NavPanel nav;
278cdf0e10cSrcweir     // End of variables declaration//GEN-END:variables
279cdf0e10cSrcweir 
280cdf0e10cSrcweir   }
281cdf0e10cSrcweir 
282cdf0e10cSrcweir class MyTableModelIDE extends AbstractTableModel {
283cdf0e10cSrcweir     ArrayList data;
284cdf0e10cSrcweir     String colNames[] = {"", "IDE Name", "IDE Location"};
285cdf0e10cSrcweir     Object[] longValues = new Object[] {Boolean.TRUE, "Name", "Location"};
286cdf0e10cSrcweir 
MyTableModelIDE(Properties properties, String [] validVersions)287cdf0e10cSrcweir     MyTableModelIDE (Properties properties, String [] validVersions) {
288cdf0e10cSrcweir         data = new ArrayList();
289cdf0e10cSrcweir         //System.out.println(properties);
290cdf0e10cSrcweir 
291cdf0e10cSrcweir         int len = validVersions.length;
292cdf0e10cSrcweir         for (int i = 0; i < len; i++) {
293cdf0e10cSrcweir             String key = validVersions[i];
294cdf0e10cSrcweir             String path = null;
295cdf0e10cSrcweir 
296cdf0e10cSrcweir             if ((path = properties.getProperty(key)) != null) {
297cdf0e10cSrcweir                 ArrayList row = new ArrayList();
298cdf0e10cSrcweir                 row.add(0, new Boolean(false));
299cdf0e10cSrcweir 
300cdf0e10cSrcweir                 row.add(1, key);
301cdf0e10cSrcweir                 if (key.length() > ((String)longValues[1]).length()) {
302cdf0e10cSrcweir                     longValues[1] = key;
303cdf0e10cSrcweir                 }
304cdf0e10cSrcweir 
305cdf0e10cSrcweir                 row.add(2, path);
306cdf0e10cSrcweir                 if (path.length() > ((String)longValues[2]).length()) {
307cdf0e10cSrcweir                     longValues[2] = path;
308cdf0e10cSrcweir                 }
309cdf0e10cSrcweir 
310cdf0e10cSrcweir                 data.add(row);
311cdf0e10cSrcweir             }
312cdf0e10cSrcweir         }
313cdf0e10cSrcweir     }// MyTableModel
314cdf0e10cSrcweir 
getColumnCount()315cdf0e10cSrcweir     public int getColumnCount() {
316cdf0e10cSrcweir         return 3;
317cdf0e10cSrcweir     }
318cdf0e10cSrcweir 
getRowCount()319cdf0e10cSrcweir     public int getRowCount() {
320cdf0e10cSrcweir         return data.size();
321cdf0e10cSrcweir     }
322cdf0e10cSrcweir 
getColumnName(int col)323cdf0e10cSrcweir     public String getColumnName(int col) {
324cdf0e10cSrcweir         return colNames[col];
325cdf0e10cSrcweir     }
326cdf0e10cSrcweir 
getValueAt(int row, int col)327cdf0e10cSrcweir     public Object getValueAt(int row, int col) {
328cdf0e10cSrcweir         if (row < 0 || row > getRowCount() ||
329cdf0e10cSrcweir             col < 0 || col > getColumnCount())
330cdf0e10cSrcweir             return null;
331cdf0e10cSrcweir 
332cdf0e10cSrcweir         ArrayList aRow = (ArrayList)data.get(row);
333cdf0e10cSrcweir         return aRow.get(col);
334cdf0e10cSrcweir     }
335cdf0e10cSrcweir 
getColumnClass(int c)336cdf0e10cSrcweir 	    public Class getColumnClass(int c) {
337cdf0e10cSrcweir 		return getValueAt(0, c).getClass();
338cdf0e10cSrcweir 	    }
339cdf0e10cSrcweir 
isCellEditable(int row, int col)340cdf0e10cSrcweir 	    public boolean isCellEditable(int row, int col) {
341cdf0e10cSrcweir 		if (col == 0) {
342cdf0e10cSrcweir 		    return true;
343cdf0e10cSrcweir 		} else {
344cdf0e10cSrcweir 		    return false;
345cdf0e10cSrcweir 		}
346cdf0e10cSrcweir 	    }
347cdf0e10cSrcweir 
setValueAt(Object value, int row, int col)348cdf0e10cSrcweir 	    public void setValueAt(Object value, int row, int col) {
349cdf0e10cSrcweir 		ArrayList aRow = (ArrayList)data.get(row);
350cdf0e10cSrcweir 		aRow.set(col, value);
351cdf0e10cSrcweir 		fireTableCellUpdated(row, col);
352cdf0e10cSrcweir 	    }
353cdf0e10cSrcweir 
getSelected()354cdf0e10cSrcweir 	    String [] getSelected() {
355cdf0e10cSrcweir 		return null;
356cdf0e10cSrcweir 	    }
357cdf0e10cSrcweir 
isAnySelected()358cdf0e10cSrcweir 	    public boolean isAnySelected() {
359cdf0e10cSrcweir 		Iterator iter = data.iterator();
360cdf0e10cSrcweir 		while (iter.hasNext()) {
361cdf0e10cSrcweir 		    ArrayList row = (ArrayList)iter.next();
362cdf0e10cSrcweir 		    if (((Boolean)row.get(0)).booleanValue() == true) {
363cdf0e10cSrcweir 			return true;
364cdf0e10cSrcweir 		    }
365cdf0e10cSrcweir 		}
366cdf0e10cSrcweir 		return false;
367cdf0e10cSrcweir 	    }
368cdf0e10cSrcweir 
369cdf0e10cSrcweir }
370cdf0e10cSrcweir 
371