KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > wizards > xhtml > TocReplaceTable


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.wizards.xhtml;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Hashtable JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.IAdaptable;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.jface.resource.ImageDescriptor;
23 import org.eclipse.pde.internal.ui.PDEPluginImages;
24 import org.eclipse.ui.model.IWorkbenchAdapter;
25
26 public class TocReplaceTable {
27
28     protected class TocReplaceEntry implements IAdaptable {
29         private String JavaDoc fHref;
30         private String JavaDoc fLabel;
31         private IResource fTocFile;
32         private IFile fEntryFile;
33         private TocReplaceEntry(String JavaDoc href, String JavaDoc title, IResource parentFile) {
34             fLabel = title;
35             fHref = href;
36             fTocFile = parentFile;
37             if (fHref != null && fTocFile != null)
38                 fEntryFile = fTocFile.getProject().getFile(fHref);
39         }
40         public String JavaDoc getHref() {
41             return fHref;
42         }
43         public IResource getTocFile() {
44             return fTocFile;
45         }
46         public String JavaDoc getLabel() {
47             return fLabel;
48         }
49         public boolean fileMissing() {
50             return fEntryFile == null || !fEntryFile.exists();
51         }
52         public IFile getEntryFile() {
53             return fEntryFile;
54         }
55         public Object JavaDoc getAdapter(Class JavaDoc adapter) {
56             if (adapter == IWorkbenchAdapter.class)
57                 return new IWorkbenchAdapter() {
58                     public Object JavaDoc[] getChildren(Object JavaDoc o) {
59                         return null;
60                     }
61                     public ImageDescriptor getImageDescriptor(Object JavaDoc object) {
62                         if (fileMissing())
63                             return PDEPluginImages.DESC_ALERT_OBJ;
64                         return PDEPluginImages.DESC_DISCOVERY;
65                     }
66                     public String JavaDoc getLabel(Object JavaDoc o) {
67                         String JavaDoc label = TocReplaceEntry.this.getLabel();
68                         String JavaDoc href = TocReplaceEntry.this.getHref();
69                         return label == null ? href : href + " (" + label + ")"; //$NON-NLS-1$ //$NON-NLS-2$
70
}
71                     public Object JavaDoc getParent(Object JavaDoc o) {
72                         return TocReplaceEntry.this.getTocFile();
73                     }
74                 };
75             return null;
76         }
77         public boolean equals(Object JavaDoc obj) {
78             if (obj instanceof TocReplaceEntry) {
79                 IPath objPath = ((TocReplaceEntry)obj).getEntryFile().getLocation();
80                 return fEntryFile.getLocation().equals(objPath);
81             }
82             return false;
83         }
84     }
85     
86     private Hashtable JavaDoc fEntries = new Hashtable JavaDoc();
87     
88     public void addToTable(String JavaDoc href, String JavaDoc title, IResource tocFile) {
89         TocReplaceEntry tro = new TocReplaceEntry(href, title, tocFile);
90         if (tro.fileMissing())
91             return; // ignore invalid entries
92
if (fEntries.containsKey(tocFile)) {
93             ArrayList JavaDoc tocList = (ArrayList JavaDoc)fEntries.get(tocFile);
94             if (!tocList.contains(tro))
95                 tocList.add(tro);
96         } else {
97             ArrayList JavaDoc tocList = new ArrayList JavaDoc();
98             tocList.add(tro);
99             fEntries.put(tocFile, tocList);
100         }
101     }
102     
103     public IResource[] getTocs() {
104         Set JavaDoc keys = fEntries.keySet();
105         Iterator JavaDoc it = keys.iterator();
106         IResource[] files = new IResource[fEntries.size()];
107         int i = 0;
108         while (it.hasNext())
109             files[i++] = (IResource)it.next();
110         return files;
111     }
112     
113     public TocReplaceEntry[] getToBeConverted(IResource file) {
114         ArrayList JavaDoc list = (ArrayList JavaDoc)fEntries.get(file);
115         return (TocReplaceEntry[]) list.toArray(new TocReplaceEntry[list.size()]);
116     }
117
118     public int numEntries() {
119         return fEntries.size();
120     }
121     
122     public void clear() {
123         fEntries.clear();
124     }
125
126 }
127
Popular Tags