| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 
 |  
/*
 * Copyright 1999 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer. 
 *   
 * - Redistribution in binary form must reproduce the above
 *   copyright notice, this list of conditions and the following
 *   disclaimer in the documentation and/or other materials
 *   provided with the distribution. 
 *   
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.  
 * 
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY
 * DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT OF OR
 * RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE OR
 * ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE 
 * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT,   
 * SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER  
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF 
 * THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS 
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 */
 
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
import javax.swing.tree.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.util.*;
import java.text.DateFormat;
 
/**
 * Assembles the UI. The UI consists of a JTreeTable and a menu.
 * The JTreeTable uses a BookmarksModel to visually represent a bookmarks
 * file stored in the Netscape file format.
 *
 * @author Scott Violet
 */
public class TreeTableExample3 {
    /** Number of instances of TreeTableExample3. */
    private static int         ttCount;
 
    /** Used to represent the model. */
    private JTreeTable         treeTable;
    /** Frame containing everything. */
    private JFrame             frame;
    /** Path created for. */
    private String             path;
 
 
    /**
     * Creates a TreeTableExample3, loading the bookmarks from the file
     * at <code>path</code>.
     */
    public TreeTableExample3(String path) {
	this.path = path;
	ttCount++;
 
	frame = createFrame();
 
	Container       cPane = frame.getContentPane();
	JMenuBar        mb = createMenuBar();
	TreeTableModel  model = createModel(path);
 
	treeTable = createTreeTable(model);
        JScrollPane sp = new JScrollPane(treeTable);
        sp.getViewport().setBackground(Color.white);
	cPane.add(sp);
 
	frame.setJMenuBar(mb);
	frame.pack();
	frame.show();
    }
 
    /**
     * Creates and returns the instanceof JTreeTable that will be used.
     */
    protected JTreeTable createTreeTable(TreeTableModel model) {
	JTreeTable       treeTable = new JTreeTable(model);
 
	treeTable.setDefaultRenderer(Date.class, new BookmarksDateRenderer());
	treeTable.setDefaultRenderer(Object.class,
				     new BookmarksStringRenderer());
	return treeTable;
    }
 
    /**
     * Creates the BookmarksModel for the file at <code>path</code>.
     */
    protected TreeTableModel createModel(String path) {
	Bookmarks bookmarks = new Bookmarks(path);
	return new BookmarksModel(bookmarks.getRoot());
    }
 
    /**
     * Creates the JFrame that will contain everything.
     */
    protected JFrame createFrame() {
	JFrame       retFrame = new JFrame("JTreeTable DB Viewer  -- " + path);
 
	retFrame.addWindowListener(new WindowAdapter() {
	    public void windowClosing(WindowEvent we) {
		frame.dispose();
		if (--ttCount == 0) {
		    System.exit(0);
		}
	    }
	});
	return retFrame;
    }
 
    /**
     * Creates a menu bar.
     */
    protected JMenuBar createMenuBar() {
        JMenu            fileMenu = new JMenu("File"); 
	JMenuItem        menuItem;
 
	menuItem = new JMenuItem("Open");
	menuItem.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent ae) {
		JFileChooser      fc = new JFileChooser(path);
		int               result = fc.showOpenDialog(frame);
 
		if (result == JFileChooser.APPROVE_OPTION) {
		    String      newPath = fc.getSelectedFile().getPath();
 
		    new TreeTableExample3(newPath);
		}
	    }
	});
	fileMenu.add(menuItem);
	fileMenu.addSeparator();
 
	menuItem = new JMenuItem("Exit"); 
	menuItem.addActionListener(new ActionListener() {
	    public void actionPerformed(ActionEvent ae) {
		System.exit(0);
	    }
	});
	fileMenu.add(menuItem); 
 
 
	// Create a menu bar
	JMenuBar        menuBar = new JMenuBar();
 
	menuBar.add(fileMenu);
 
	// Menu for the look and feels (lafs).
	UIManager.LookAndFeelInfo[]        lafs = UIManager.
	                                    getInstalledLookAndFeels();
	ButtonGroup                        lafGroup = new ButtonGroup();
 
	JMenu          optionsMenu = new JMenu("Options");
 
	menuBar.add(optionsMenu);
 
	for(int i = 0; i < lafs.length; i++) {
	    JRadioButtonMenuItem rb = new JRadioButtonMenuItem(lafs[i].
							       getName()); 
	    optionsMenu.add(rb);
	    rb.setSelected(UIManager.getLookAndFeel().getName().equals
			   (lafs[i].getName()));
	    rb.putClientProperty("UIKey", lafs[i]);
	    rb.addItemListener(new ItemListener() {
	        public void itemStateChanged(ItemEvent ae) {
	            JRadioButtonMenuItem rb2 = (JRadioButtonMenuItem)ae.
			                       getSource();
	            if(rb2.isSelected()) {
		        UIManager.LookAndFeelInfo       info =
                                      (UIManager.LookAndFeelInfo)
			               rb2.getClientProperty("UIKey");
		        try {
		            UIManager.setLookAndFeel(info.getClassName());
		            SwingUtilities.updateComponentTreeUI(frame);
			}
			catch (Exception e) {
		             System.err.println("unable to set UI " +
						e.getMessage());
			}
	            }
	        }
	    });
	    lafGroup.add(rb);
	}
	return menuBar;
    }
 
 
    /**
     * The renderer used for Dates in the TreeTable. The only thing it does,
     * is to format a null date as '---'.
     */
    private static class BookmarksDateRenderer extends
                   DefaultTableCellRenderer {
	DateFormat formatter;
	public BookmarksDateRenderer() { super(); }
 
	public void setValue(Object value) { 
	    if (formatter==null) {
		formatter = DateFormat.getDateInstance(); 
	    }
	    setText((value == null) ? "---" : formatter.format(value)); 
	}
    }
 
 
    /**
     * The renderer used for String in the TreeTable. The only thing it does,
     * is to format a null String as '---'.
     */
    static class BookmarksStringRenderer extends DefaultTableCellRenderer {
	public BookmarksStringRenderer() { super(); }
 
	public void setValue(Object value) { 
	    setText((value == null) ? "---" : value.toString());
	}
    }
 
 
    public static void main(String[] args) {
	if (args.length > 0) {
	    // User is specifying the bookmark file to show.
	    for (int counter = args.length - 1; counter >= 0; counter--) {
		new TreeTableExample3(args[counter]);
	    }
	}
	else {
	    // No file specified, see if the user has one in their home
	    // directory.
	    String            path;
 
	    try {
		path = System.getProperty("user.home");
		if (path != null) {
		    path += File.separator + ".netscape" + File.separator +
			    "bookmarks.xml";
		    File file = new File(path);
		    if (!file.exists()) {
			path = null;
		    }
		}
	    }
	    catch (Throwable th) {
		path = null;
	    }
	    if (path == null) {
		// None available, use a default.
		path = "bookmarks.xml";
	    }
            new TreeTableExample3(path);
	}
    }
} | 
Partager