KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > ui > model > UpdateModel


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.ui.model;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.Iterator JavaDoc;
16 import java.util.Vector JavaDoc;
17
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.update.configurator.*;
20 import org.eclipse.update.internal.ui.*;
21 import org.eclipse.update.operations.*;
22
23 public class UpdateModel implements IAdaptable {
24     private Vector JavaDoc bookmarks = new Vector JavaDoc();
25     private Vector JavaDoc listeners = new Vector JavaDoc();
26     private static final String JavaDoc BOOKMARK_FILE = "bookmarks.xml"; //$NON-NLS-1$
27

28     public UpdateModel() {
29         reset();
30     }
31     
32     public void reset() {
33         // load bookmarks
34
bookmarks.clear();
35         BookmarkUtil.parse(getBookmarksFileName(), bookmarks);
36     }
37     
38     private String JavaDoc getBookmarksFileName() {
39         URL platformXML = ConfiguratorUtils.getCurrentPlatformConfiguration().getConfigurationLocation();
40         if (!"file".equals(platformXML.getProtocol())) { //$NON-NLS-1$
41
IPath path = UpdateUI.getDefault().getStateLocation();
42             path = path.append(BOOKMARK_FILE);
43             return path.toOSString();
44         } else {
45             File f = new File(platformXML.getFile());
46             f = new File(f.getParentFile(), BOOKMARK_FILE);
47             return f.getAbsolutePath();
48         }
49     }
50     
51     public void shutdown() {
52         saveBookmarks();
53     }
54     
55     public void saveBookmarks() {
56         BookmarkUtil.store(getBookmarksFileName(), bookmarks);
57     }
58     
59     
60     public void addBookmark(NamedModelObject bookmark) {
61         bookmarks.add(bookmark);
62         bookmark.setModel(this);
63         fireObjectsAdded(null, new Object JavaDoc []{bookmark});
64     }
65     
66     public void removeBookmark(NamedModelObject bookmark) {
67         bookmarks.remove(bookmark);
68         bookmark.setModel(null);
69         fireObjectsRemoved(null, new Object JavaDoc []{bookmark});
70     }
71     
72     public NamedModelObject [] getBookmarks() {
73         return (NamedModelObject[])bookmarks.toArray(new NamedModelObject[bookmarks.size()]);
74     }
75     
76     public SiteBookmark [] getBookmarkLeafs() {
77         return BookmarkUtil.getBookmarks(bookmarks);
78     }
79     
80     public BookmarkFolder getFolder(IPath path) {
81         return BookmarkUtil.getFolder(bookmarks, path);
82     }
83     
84     public void addUpdateModelChangedListener(IUpdateModelChangedListener listener) {
85         if (!listeners.contains(listener))
86            listeners.add(listener);
87     }
88
89
90     public void removeUpdateModelChangedListener(IUpdateModelChangedListener listener) {
91         if (listeners.contains(listener))
92             listeners.remove(listener);
93     }
94     
95     public void fireObjectsAdded(Object JavaDoc parent, Object JavaDoc [] children) {
96         for (Iterator JavaDoc iter=listeners.iterator();
97                 iter.hasNext();) {
98             IUpdateModelChangedListener listener = (IUpdateModelChangedListener)iter.next();
99             listener.objectsAdded(parent, children);
100         }
101     }
102
103
104     public void fireObjectsRemoved(Object JavaDoc parent, Object JavaDoc [] children) {
105         for (Iterator JavaDoc iter=listeners.iterator();
106                 iter.hasNext();) {
107             IUpdateModelChangedListener listener = (IUpdateModelChangedListener)iter.next();
108             listener.objectsRemoved(parent, children);
109         }
110     }
111     
112     public void fireObjectChanged(Object JavaDoc object, String JavaDoc property) {
113         for (Iterator JavaDoc iter=listeners.iterator();
114             iter.hasNext();) {
115             IUpdateModelChangedListener listener = (IUpdateModelChangedListener)iter.next();
116             listener.objectChanged(object, property);
117         }
118     }
119     public Object JavaDoc getAdapter(Class JavaDoc key) {
120         return null;
121     }
122 }
123
Popular Tags