Revision 44872 trunk/org.gvsig.desktop/org.gvsig.desktop.library/org.gvsig.symbology/org.gvsig.symbology.swing/org.gvsig.symbology.swing.impl/src/main/java/org/gvsig/symbology/swing/impl/DefaultSymbologySwingManager.java

View differences:

DefaultSymbologySwingManager.java
60 60
 * @author gvSIG Team
61 61
 * @version $Id$
62 62
 */
63
@SuppressWarnings("UseSpecificCatch")
63 64
public class DefaultSymbologySwingManager implements
64 65
        SymbologySwingManager {
65 66

  
66
    private static final Logger logger = LoggerFactory.getLogger(DefaultSymbologySwingManager.class);
67
  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultSymbologySwingManager.class);
67 68

  
68
    private SymbologyManager manager;
69
    private I18nManager i18nmanager = null;
70
    private SymbologyWindowManager windowManager;
71
    private ColorTablesFactory colorTablesFactory;
69
  private static Map<Integer, List<Class<? extends TypeSymbolEditor>>> symbolEditorRegistry;
72 70

  
73
    private static Map<Integer, List<Class<? extends TypeSymbolEditor>>> symbolEditorRegistry;
74
    private List<Class<? extends ILegendPanel>> legendEditorRegistry = null;
75
    private List<Class<? extends ILabelingStrategyPanel>> labelingEditorRegistry = null;
76
    private Map<String,LabelClassEditorFactory> labelClassEditorFactories = new HashMap<String, LabelClassEditorFactory>();
77
    private List<Action> optionalActionsOfLegendsPanel;
71
  private final SymbologyManager manager;
72
  private final Map<String, LabelClassEditorFactory> labelClassEditorFactories = new HashMap<>();
78 73

  
79
    public DefaultSymbologySwingManager() {
80
        this.manager = SymbologyLocator.getSymbologyManager();
81
        this.windowManager = new DefaultSymbologyWindowManager();
82
        this.colorTablesFactory = new DefaultColorTablesFactory();
74
  private I18nManager i18nmanager = null;
75
  private SymbologyWindowManager windowManager;
76
  private ColorTablesFactory colorTablesFactory;
83 77

  
84
        this.legendEditorRegistry = new ArrayList<Class<? extends ILegendPanel>>();
85
        this.labelingEditorRegistry = new ArrayList<Class<? extends ILabelingStrategyPanel>>();
86
    }
78
  private List<Class<? extends ILegendPanel>> legendEditorRegistry = null;
79
  private List<Class<? extends ILabelingStrategyPanel>> labelingEditorRegistry = null;
80
  private List<Action> optionalActionsOfLegendsPanel;
87 81

  
88
    public SymbologyManager getManager() {
89
        return this.manager;
90
    }
82
  public DefaultSymbologySwingManager() {
83
    this.manager = SymbologyLocator.getSymbologyManager();
84
    this.windowManager = new DefaultSymbologyWindowManager();
85
    this.colorTablesFactory = new DefaultColorTablesFactory();
91 86

  
92
    public String getTranslation(String key) {
93
        if (this.i18nmanager == null) {
94
            this.i18nmanager = ToolsLocator.getI18nManager();
95
        }
96
        return this.i18nmanager.getTranslation(key);
97
    }
87
    this.legendEditorRegistry = new ArrayList<>();
88
    this.labelingEditorRegistry = new ArrayList<>();
89
  }
98 90

  
99
    public void registerWindowManager(SymbologyWindowManager manager) {
100
        this.windowManager = manager;
101
    }
91
  @Override
92
  public SymbologyManager getManager() {
93
    return this.manager;
94
  }
102 95

  
103
    public SymbologyWindowManager getWindowManager() {
104
        return this.windowManager;
96
  @Override
97
  public String getTranslation(String key) {
98
    if (this.i18nmanager == null) {
99
      this.i18nmanager = ToolsLocator.getI18nManager();
105 100
    }
101
    return this.i18nmanager.getTranslation(key);
102
  }
106 103

  
107
    public void setColorTablesFactory(ColorTablesFactory factory) {
108
        colorTablesFactory = factory;
109
    }
104
  @Override
105
  public void registerWindowManager(SymbologyWindowManager manager) {
106
    this.windowManager = manager;
107
  }
110 108

  
111
    public List<ColorTablePainter> createColorTables() {
112
        if (colorTablesFactory != null) {
113
            return colorTablesFactory.createColorTables();
114
        }
115
        return null;
116
    }
109
  @Override
110
  public SymbologyWindowManager getWindowManager() {
111
    return this.windowManager;
112
  }
117 113

  
118
    public ColorTablesFactory getColorTablesFactory() {
119
        return this.colorTablesFactory;
120
    }
114
  @Override
115
  public void setColorTablesFactory(ColorTablesFactory factory) {
116
    colorTablesFactory = factory;
117
  }
121 118

  
122
    /**
123
     * @deprecated use registerTypeSymbolEditor
124
     */
125
    public void addSymbolEditorPanel(Class abstractTypeSymbolEditorPanelClass, int shapeType) {
126
        Class<? extends TypeSymbolEditor> symbolEditor = abstractTypeSymbolEditorPanelClass;
127
        this.registerSymbolEditor(symbolEditor, shapeType);
119
  @Override
120
  public List<ColorTablePainter> createColorTables() {
121
    if (colorTablesFactory != null) {
122
      return colorTablesFactory.createColorTables();
128 123
    }
124
    return null;
125
  }
129 126

  
130
    public void registerSymbolEditor(Class<? extends TypeSymbolEditor> symbolEditor, int shapeType) {
131
        if (symbolEditorRegistry == null) {
132
            symbolEditorRegistry = new HashMap<Integer, List<Class<? extends TypeSymbolEditor>>>();
133
        }
127
  @Override
128
  public ColorTablesFactory getColorTablesFactory() {
129
    return this.colorTablesFactory;
130
  }
134 131

  
135
        Integer key = new Integer(shapeType);
136
        List<Class<? extends TypeSymbolEditor>> l = symbolEditorRegistry.get(key);
137
        if (l == null) {
138
            l = new ArrayList<Class<? extends TypeSymbolEditor>>();
139
        }
140
        l.add(symbolEditor);
132
  /**
133
   * @param abstractTypeSymbolEditorPanelClass
134
   * @param shapeType
135
   * @deprecated use registerTypeSymbolEditor
136
   */
137
  public void addSymbolEditorPanel(Class abstractTypeSymbolEditorPanelClass, int shapeType) {
138
    Class<? extends TypeSymbolEditor> symbolEditor = abstractTypeSymbolEditorPanelClass;
139
    this.registerSymbolEditor(symbolEditor, shapeType);
140
  }
141 141

  
142
        symbolEditorRegistry.put(key, l);
142
  @Override
143
  public void registerSymbolEditor(Class<? extends TypeSymbolEditor> symbolEditor, int shapeType) {
144
    if (symbolEditorRegistry == null) {
145
      symbolEditorRegistry = new HashMap<>();
143 146
    }
144 147

  
145
    public List<Class<? extends TypeSymbolEditor>> getSymbolEditorClassesByGeometryType(GeometryType geometryType) {
146
        if (symbolEditorRegistry == null) {
147
            return Collections.emptyList();
148
        }
149
        Iterator it = symbolEditorRegistry.keySet().iterator();
150
        while (it.hasNext()) {
151
            int currentType = (Integer) it.next();
152
            if (geometryType.isTypeOf(currentType)) {
153
                return (List) symbolEditorRegistry.get(currentType);
154
            }
155
        }
156
        return Collections.emptyList();
148
    Integer key = shapeType;
149
    List<Class<? extends TypeSymbolEditor>> l = symbolEditorRegistry.get(key);
150
    if (l == null) {
151
      l = new ArrayList<>();
157 152
    }
153
    l.add(symbolEditor);
158 154

  
159
    public void registerLegendEditor(Class<? extends ILegendPanel> legendEditor) {
160
        if (this.legendEditorRegistry == null) {
161
            this.legendEditorRegistry = new ArrayList<Class<? extends ILegendPanel>>();
162
        }
163
        if (!this.legendEditorRegistry.contains(legendEditor)) {
164
            this.legendEditorRegistry.add(legendEditor);
165
        }
155
    symbolEditorRegistry.put(key, l);
156
  }
157

  
158
  @Override
159
  public List<Class<? extends TypeSymbolEditor>> getSymbolEditorClassesByGeometryType(GeometryType geometryType) {
160
    if (symbolEditorRegistry == null) {
161
      return Collections.emptyList();
166 162
    }
163
    Iterator it = symbolEditorRegistry.keySet().iterator();
164
    while (it.hasNext()) {
165
      int currentType = (Integer) it.next();
166
      if (geometryType.isTypeOf(currentType)) {
167
        return (List) symbolEditorRegistry.get(currentType);
168
      }
169
    }
170
    return Collections.emptyList();
171
  }
167 172

  
168
    public List<Class<? extends ILegendPanel>> getLegendEditorClasses() {
169
        return Collections.unmodifiableList(this.legendEditorRegistry);
173
  @Override
174
  public void registerLegendEditor(Class<? extends ILegendPanel> legendEditor) {
175
    if (this.legendEditorRegistry == null) {
176
      this.legendEditorRegistry = new ArrayList<>();
170 177
    }
178
    if (!this.legendEditorRegistry.contains(legendEditor)) {
179
      this.legendEditorRegistry.add(legendEditor);
180
    }
181
  }
171 182

  
172
    public List<ILegendPanel> getLegendEditors(FLayer layer) {
173
        List<ILegendPanel> editors = new ArrayList<ILegendPanel>();
174
        Class<? extends ILegendPanel> legendEditorClass = null;
175
        ILegendPanel editor = null;
183
  @Override
184
  public List<Class<? extends ILegendPanel>> getLegendEditorClasses() {
185
    return Collections.unmodifiableList(this.legendEditorRegistry);
186
  }
176 187

  
177
        for (int i = 0; i < legendEditorRegistry.size(); i++) {
178
            legendEditorClass = legendEditorRegistry.get(i);
179
            try {
180
                editor = null;
181
                editor = (ILegendPanel) legendEditorClass.newInstance();
182
            } catch (Exception e) {
183
                logger.info("Unable to instantiate legend editor.", e);
184
            }
185
            if( layer.isAvailable() ) {
186
                if (editor != null && editor.isSuitableFor(layer)) {
187
                    editors.add(editor);
188
                }
189
            } else {
190
                try {
191
                    if (editor != null && editor.isSuitableFor(layer)) {
192
                        editors.add(editor);
193
                    }
194
                } catch(Throwable th) {
188
  @Override
189
  public List<ILegendPanel> getLegendEditors(FLayer layer) {
190
    List<ILegendPanel> editors = new ArrayList<>();
195 191

  
196
                }
197
            }
192
    for (int i = 0; i < legendEditorRegistry.size(); i++) {
193
      Class<? extends ILegendPanel> legendEditorClass = null;
194
      try {
195
        legendEditorClass = legendEditorRegistry.get(i);
196
        ILegendPanel editor = (ILegendPanel) legendEditorClass.newInstance();
197
        if (editor.isSuitableFor(layer)) {
198
          editors.add(editor);
198 199
        }
199
        return editors;
200
      } catch (Throwable e) {
201
        LOGGER.warn("Unable to initialize legend editor ("+(legendEditorClass==null?"NULL":legendEditorClass.getName())+").", e);
202
      }
200 203
    }
204
    return editors;
205
  }
201 206

  
202
    public void registerLabelingEditor(Class<? extends ILabelingStrategyPanel> labelingEditor) {
203
        if (!this.labelingEditorRegistry.contains(labelingEditor)) {
204
            this.labelingEditorRegistry.add(labelingEditor);
205
        }
207
  @Override
208
  public void registerLabelingEditor(Class<? extends ILabelingStrategyPanel> labelingEditor) {
209
    if (!this.labelingEditorRegistry.contains(labelingEditor)) {
210
      this.labelingEditorRegistry.add(labelingEditor);
206 211
    }
212
  }
207 213

  
208
    public List<ILabelingStrategyPanel> getLabelingEditors() {
209
        List<ILabelingStrategyPanel> labelingEditors = new ArrayList<ILabelingStrategyPanel>();
210
        Iterator<Class<? extends ILabelingStrategyPanel>> it = this.labelingEditorRegistry.iterator();
211
        while (it.hasNext()) {
212
            Class<? extends ILabelingStrategyPanel> labelingEditorClass = it.next();
213
            try {
214
                ILabelingStrategyPanel labelingEditor = labelingEditorClass.newInstance();
215
                labelingEditors.add(labelingEditor);
216
            } catch (Exception ex) {
217
                String msg = "Can't create the labeling editor associated to '"+labelingEditorClass.getName()+"'.";
218
                logger.warn(msg,ex);
219
                throw new RuntimeException(msg,ex);
220
            }
221
        }
222
        return labelingEditors;
214
  @Override
215
  public List<ILabelingStrategyPanel> getLabelingEditors() {
216
    List<ILabelingStrategyPanel> labelingEditors = new ArrayList<>();
217
    Iterator<Class<? extends ILabelingStrategyPanel>> it = this.labelingEditorRegistry.iterator();
218
    while (it.hasNext()) {
219
      Class<? extends ILabelingStrategyPanel> labelingEditorClass = null;
220
      try {
221
        labelingEditorClass = it.next();
222
        ILabelingStrategyPanel labelingEditor = labelingEditorClass.newInstance();
223
        labelingEditors.add(labelingEditor);
224
      } catch (Throwable ex) {
225
        String msg = "Can't create the labeling editor associated to '" + (labelingEditorClass==null?"NULL":labelingEditorClass.getName()) + "'.";
226
        LOGGER.warn(msg, ex);
227
      }
223 228
    }
229
    return labelingEditors;
230
  }
224 231

  
225
    public void registerLabelClassEditor(LabelClassEditorFactory factory) {
226
        this.labelClassEditorFactories.put(factory.getID().toLowerCase(), factory);
227
    }
232
  @Override
233
  public void registerLabelClassEditor(LabelClassEditorFactory factory) {
234
    this.labelClassEditorFactories.put(factory.getID().toLowerCase(), factory);
235
  }
228 236

  
229
    public Collection<LabelClassEditorFactory> getLabelClassEditorFactories() {
230
        return Collections.unmodifiableCollection( this.labelClassEditorFactories.values());
231
    }
237
  @Override
238
  public Collection<LabelClassEditorFactory> getLabelClassEditorFactories() {
239
    return Collections.unmodifiableCollection(this.labelClassEditorFactories.values());
240
  }
232 241

  
233
    public LabelClassEditorFactory getLabelClassEditorFactory(String id) {
234
        if( id==null ) {
235
            return null;
236
        }
237
        return this.labelClassEditorFactories.get(id.toLowerCase());
242
  @Override
243
  public LabelClassEditorFactory getLabelClassEditorFactory(String id) {
244
    if (id == null) {
245
      return null;
238 246
    }
247
    return this.labelClassEditorFactories.get(id.toLowerCase());
248
  }
239 249

  
240
    public LabelClassEditorFactory getLabelClassEditorFactory(ILabelClass labelClass) {
241
        Iterator<LabelClassEditorFactory> it = this.labelClassEditorFactories.values().iterator();
242
        while( it.hasNext() ) {
243
            LabelClassEditorFactory labelClassEditorFactory = it.next();
244
            if( labelClassEditorFactory!=null && labelClassEditorFactory.accept(labelClass.getClass()) ) {
245
                return labelClassEditorFactory;
246
            }
250
  @Override
251
  public LabelClassEditorFactory getLabelClassEditorFactory(ILabelClass labelClass) {
252
    Iterator<LabelClassEditorFactory> it = this.labelClassEditorFactories.values().iterator();
253
    while (it.hasNext()) {
254
      LabelClassEditorFactory labelClassEditorFactory = null;
255
      try {
256
        labelClassEditorFactory = it.next();
257
        if ( labelClassEditorFactory.accept(labelClass.getClass())) {
258
          return labelClassEditorFactory;
247 259
        }
248
        return null;
260
      } catch(Throwable th) {
261
        LOGGER.warn("Can't check factory '"+(labelClassEditorFactory==null?"NULL":labelClassEditorFactory.getName())+"'.", th);
262
      }
249 263
    }
264
    return null;
265
  }
250 266

  
251
    public LabelClassEditor createLabelClassEditor(ILabelClass labelClass, FeatureStore store) {
252
        LabelClassEditorFactory f = this.getLabelClassEditorFactory(labelClass);
253
        if( f == null ) {
254
            return null;
255
        }
256
        return f.createEditor(labelClass, store);
267
  @Override
268
  public LabelClassEditor createLabelClassEditor(ILabelClass labelClass, FeatureStore store) {
269
    LabelClassEditorFactory f = this.getLabelClassEditorFactory(labelClass);
270
    if (f == null) {
271
      return null;
257 272
    }
273
    return f.createEditor(labelClass, store);
274
  }
258 275

  
259
    @Override
260
    public Iterable<Action> getOptionalActionOfLegendsPanel() {
261
        if( this.optionalActionsOfLegendsPanel==null ) {
262
            this.optionalActionsOfLegendsPanel = new ArrayList<>();
263
        }
264
        return this.optionalActionsOfLegendsPanel;
276
  @Override
277
  public Iterable<Action> getOptionalActionOfLegendsPanel() {
278
    if (this.optionalActionsOfLegendsPanel == null) {
279
      this.optionalActionsOfLegendsPanel = new ArrayList<>();
265 280
    }
281
    return this.optionalActionsOfLegendsPanel;
282
  }
266 283

  
267
    @Override
268
    public void addOptionalActionToLegendsPanel(Action action) {
269
        if( this.optionalActionsOfLegendsPanel==null ) {
270
            this.optionalActionsOfLegendsPanel = new ArrayList<>();
271
        }
272
        this.optionalActionsOfLegendsPanel.add(action);
284
  @Override
285
  public void addOptionalActionToLegendsPanel(Action action) {
286
    if (this.optionalActionsOfLegendsPanel == null) {
287
      this.optionalActionsOfLegendsPanel = new ArrayList<>();
273 288
    }
289
    this.optionalActionsOfLegendsPanel.add(action);
290
  }
274 291

  
275 292
}

Also available in: Unified diff