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