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