001: /*
002: * Created on 29-Sep-2004
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package net.refractions.udig.printing.ui.internal;
008:
009: import java.util.ArrayList;
010: import java.util.HashMap;
011: import java.util.List;
012: import java.util.Map;
013:
014: import net.refractions.udig.core.internal.ExtensionPointList;
015: import net.refractions.udig.printing.model.PrintingModelPlugin;
016: import net.refractions.udig.printing.ui.TemplateFactory;
017: import net.refractions.udig.printing.ui.internal.editor.BoxAction;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.core.runtime.IExtension;
022: import org.eclipse.core.runtime.IExtensionPoint;
023: import org.eclipse.core.runtime.IExtensionRegistry;
024: import org.eclipse.core.runtime.IStatus;
025: import org.eclipse.core.runtime.Platform;
026: import org.eclipse.core.runtime.Status;
027: import org.eclipse.ui.IWorkbenchPart;
028: import org.eclipse.ui.plugin.AbstractUIPlugin;
029: import org.osgi.framework.BundleContext;
030:
031: /**
032: * @author Richard Gould
033: *
034: * TODO To change the template for this generated type comment go to Window -
035: * Preferences - Java - Code Style - Code Templates
036: */
037: public class PrintingPlugin extends AbstractUIPlugin {
038: private static PrintingPlugin plugin;
039:
040: private static List<BoxAction> extensionActions;
041:
042: private Map<String, TemplateFactory> templateFactories;
043:
044: private List<BoxFactory> boxes;
045:
046: public static final String TEMPLATE_FACTORIES_ID = "net.refractions.udig.printing.ui.templateFactories"; //$NON-NLS-1$
047: public static final String PREF_DEFAULT_TEMPLATE = "udig.preferences.defaultTemplate"; //$NON-NLS-1$
048:
049: public static final String ID = "net.refractions.udig.printing.ui"; //$NON-NLS-1$
050:
051: private static final String boxEditActionGroupName = "editActionGroup"; //$NON-NLS-1$
052:
053: public PrintingPlugin() {
054: super ();
055: plugin = this ;
056: }
057:
058: public static PrintingPlugin getDefault() {
059: return plugin;
060: }
061:
062: public Map<String, TemplateFactory> getTemplateFactories() {
063: if (templateFactories == null) {
064: templateFactories = gatherTemplateFactories();
065: }
066: return templateFactories;
067: }
068:
069: private Map<String, TemplateFactory> gatherTemplateFactories() {
070: IExtensionRegistry registry = Platform.getExtensionRegistry();
071:
072: IExtensionPoint extensionPoint = registry
073: .getExtensionPoint(TEMPLATE_FACTORIES_ID);
074:
075: IExtension[] extensions = extensionPoint.getExtensions();
076:
077: HashMap<String, TemplateFactory> results = new HashMap<String, TemplateFactory>();
078:
079: for (int i = 0; i < extensions.length; i++) {
080: IConfigurationElement[] elements = extensions[i]
081: .getConfigurationElements();
082:
083: for (int j = 0; j < elements.length; j++) {
084: try {
085: Object templateFactory = elements[j]
086: .createExecutableExtension("class"); //$NON-NLS-1$
087: if (templateFactory instanceof TemplateFactory) {
088: String id = elements[j].getAttribute("id"); //$NON-NLS-1$
089: results.put(id,
090: (TemplateFactory) templateFactory);
091: } else {
092: log(
093: "Bad extension! Declared class is not of type TemplateFactory!", null); //$NON-NLS-1$
094: }
095: } catch (CoreException e) {
096: e.printStackTrace();
097: }
098: }
099: }
100:
101: return results;
102: }
103:
104: /*
105: * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
106: */
107: public void start(BundleContext context) throws Exception {
108: super .start(context);
109: }
110:
111: /*
112: * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
113: */
114: public void stop(BundleContext context) throws Exception {
115: super .stop(context);
116: }
117:
118: /**
119: * Gets the Actions that need to be added to the PageEditor
120: *
121: * @return
122: */
123: public synchronized static List<BoxAction> getBoxExtensionActions(
124: IWorkbenchPart part) {
125: if (extensionActions == null) {
126: extensionActions = new ArrayList<BoxAction>();
127: List<IConfigurationElement> list = ExtensionPointList
128: .getExtensionPointList(PrintingModelPlugin.BOX_PRINTER_EXTENSION_ID);
129: for (IConfigurationElement element : list) {
130: if (element.getName().equals(
131: PrintingPlugin.boxEditActionGroupName)) {
132: String acceptable = element
133: .getAttribute("acceptable"); //$NON-NLS-1$
134: IConfigurationElement[] children = element
135: .getChildren("editAction"); //$NON-NLS-1$
136: for (IConfigurationElement element2 : children) {
137: extensionActions.add(new BoxAction(part,
138: element2, acceptable));
139: }
140: }
141: }
142: }
143: return extensionActions;
144: }
145:
146: /**
147: * Gets all the boxes that will appear in the tool bar.
148: *
149: * @return all the boxes that will appear in the tool bar.
150: */
151: public List<BoxFactory> getVisibleBoxes() {
152: if (boxes == null) {
153: boxes = gatherBoxes(ExtensionPointList
154: .getExtensionPointList(PrintingModelPlugin.BOX_PRINTER_EXTENSION_ID));
155: }
156: ArrayList<BoxFactory> visibleBoxes = new ArrayList<BoxFactory>();
157:
158: for (BoxFactory entry : boxes) {
159: if (entry.isVisible()) {
160: visibleBoxes.add(entry);
161: }
162: }
163:
164: return visibleBoxes;
165: }
166:
167: private List<BoxFactory> gatherBoxes(
168: List<IConfigurationElement> list) {
169:
170: List<BoxFactory> results = new ArrayList<BoxFactory>();
171:
172: for (IConfigurationElement element : list) {
173: if (!element.getName().equals("boxprinter")) //$NON-NLS-1$
174: continue;
175: results.add(new BoxFactory(element));
176: }
177:
178: return results;
179: }
180:
181: /**
182: * Writes an info log in the plugin's log.
183: * <p>
184: * This should be used for user level messages.
185: * </p>
186: */
187: public static void log(String message, Throwable e) {
188: getDefault().getLog().log(
189: new Status(IStatus.INFO, ID, IStatus.OK, message, e));
190: }
191:
192: public final static String TRACE_PRINTING = "net.refractions.udig.printing.ui/debug/printing"; //$NON-NLS-1$
193:
194: /**
195: * Performs the Platform.getDebugOption true check on the provided trace
196: *
197: * @param trace constant, defined in the Trace class
198: * @return true if -debug is on for this plugin
199: */
200: public static boolean isDebugging(final String trace) {
201: return getDefault().isDebugging()
202: && "true".equalsIgnoreCase(Platform.getDebugOption(trace)); //$NON-NLS-1$
203: }
204:
205: /**
206: * Outputs a message or an Exception if the current plug-in is debugging.
207: *
208: * @param message if not null, message will be sent to standard out
209: * @param e if not null, e.printStackTrace() will be called.
210: */
211: public static void trace(String message, Throwable e) {
212: if (getDefault().isDebugging()) {
213: if (message != null)
214: System.out.println(message);
215: if (e != null)
216: e.printStackTrace();
217: }
218: }
219: }
|