001: package net.refractions.udig.project.internal.commands;
002:
003: import java.awt.Color;
004: import java.util.ArrayList;
005: import java.util.List;
006:
007: import net.refractions.udig.catalog.IGeoResource;
008: import net.refractions.udig.catalog.IGeoResourceInfo;
009: import net.refractions.udig.project.IMap;
010: import net.refractions.udig.project.IProject;
011: import net.refractions.udig.project.IProjectElement;
012: import net.refractions.udig.project.ProjectBlackboardConstants;
013: import net.refractions.udig.project.command.AbstractCommand;
014: import net.refractions.udig.project.command.UndoableMapCommand;
015: import net.refractions.udig.project.internal.Layer;
016: import net.refractions.udig.project.internal.LayerFactory;
017: import net.refractions.udig.project.internal.Map;
018: import net.refractions.udig.project.internal.Messages;
019: import net.refractions.udig.project.internal.Project;
020: import net.refractions.udig.project.internal.ProjectFactory;
021: import net.refractions.udig.project.internal.ProjectPlugin;
022: import net.refractions.udig.project.internal.Trace;
023: import net.refractions.udig.project.preferences.PreferenceConstants;
024:
025: import org.eclipse.core.runtime.IProgressMonitor;
026: import org.eclipse.jface.preference.IPreferenceStore;
027: import org.eclipse.jface.preference.PreferenceConverter;
028: import org.eclipse.swt.graphics.RGB;
029:
030: public class CreateMapCommand extends AbstractCommand implements
031: UndoableMapCommand {
032:
033: /** name of the new map * */
034: String name;
035:
036: /** resources / layers * */
037: List<IGeoResource> resources;
038:
039: /** owning project * */
040: Project owner;
041:
042: /** created map * */
043: Map map;
044:
045: public CreateMapCommand(String name, List<IGeoResource> resources,
046: IProject owner) {
047: this .name = name;
048: this .resources = resources;
049: this .owner = (Project) owner;
050: }
051:
052: public void run(IProgressMonitor monitor) throws Exception {
053: if (owner == null) {
054: // default to current project
055: owner = ProjectPlugin.getPlugin().getProjectRegistry()
056: .getCurrentProject();
057: }
058: if (name == null) {
059: if (resources.size() > 0) {
060: IGeoResource resource = resources.get(0);
061:
062: IGeoResourceInfo info = resource.getInfo(monitor);
063: if (info != null) {
064: name = info.getTitle();
065: if (name == null)
066: name = info.getName();
067: }
068: }
069: if (name == null) {
070: name = Messages.CreateMapCommand_defaultname;
071: }
072:
073: int i = 1;
074: String newName = name;
075: while (nameTaken(newName)) {
076: i++;
077: newName = name + " " + i; //$NON-NLS-1$
078: }
079: name = newName;
080: }
081: // create the map
082: map = ProjectFactory.eINSTANCE.createMap(owner, name,
083: new ArrayList());
084:
085: IPreferenceStore store = ProjectPlugin.getPlugin()
086: .getPreferenceStore();
087: RGB background = PreferenceConverter.getColor(store,
088: PreferenceConstants.P_BACKGROUND);
089: map.getBlackboard().put(
090: ProjectBlackboardConstants.MAP__BACKGROUND_COLOR,
091: new Color(background.red, background.green,
092: background.blue));
093:
094: LayerFactory layerFactory = map.getLayerFactory();
095: List<Layer> toAdd = new ArrayList<Layer>(resources.size());
096: for (IGeoResource resource : resources) {
097: Layer layer = layerFactory.createLayer(resource);
098: toAdd.add(layer);
099: }
100:
101: map.getLayersInternal().addAll(toAdd);
102:
103: trace(toAdd);
104:
105: }
106:
107: private void trace(List<Layer> toAdd) {
108: if (ProjectPlugin.isDebugging(Trace.COMMANDS)) {
109: List<String> ids = new ArrayList<String>();
110: for (Layer layer : toAdd) {
111: ids.add(layer.getID().toString());
112: }
113: ProjectPlugin
114: .trace(
115: getClass(),
116: "Created Map: " + map.getName() + " and added Layers: " + ids, null); //$NON-NLS-1$ //$NON-NLS-2$
117: }
118: }
119:
120: private boolean nameTaken(String newName) {
121: for (IProjectElement element : owner.getElements()) {
122: if (newName.equals(element.getName()))
123: return true;
124: }
125: return false;
126: }
127:
128: public String getName() {
129: return Messages.CreateMapCommand_commandname;
130: }
131:
132: public IMap getCreatedMap() {
133: return map;
134: }
135:
136: public void rollback(IProgressMonitor monitor) throws Exception {
137: owner.getElementsInternal().remove(map);
138: map.eResource().unload();
139: }
140:
141: }
|