Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.gui / src / main / java / es / unex / sextante / gui / toolbox / AlgorithmsPanel.java @ 412

History | View | Annotate | Download (27.9 KB)

1

    
2

    
3
package es.unex.sextante.gui.toolbox;
4

    
5
import java.awt.BorderLayout;
6
import java.awt.Cursor;
7
import java.awt.Font;
8
import java.awt.Rectangle;
9
import java.awt.event.ActionEvent;
10
import java.awt.event.ActionListener;
11
import java.awt.event.ItemEvent;
12
import java.awt.event.ItemListener;
13
import java.awt.event.KeyEvent;
14
import java.awt.event.KeyListener;
15
import java.awt.event.MouseAdapter;
16
import java.awt.event.MouseEvent;
17
import java.awt.event.MouseListener;
18
import java.text.Collator;
19
import java.util.ArrayList;
20
import java.util.Enumeration;
21
import java.util.HashMap;
22
import java.util.Iterator;
23
import java.util.Set;
24

    
25
import javax.swing.ImageIcon;
26
import javax.swing.JCheckBoxMenuItem;
27
import javax.swing.JMenuItem;
28
import javax.swing.JPanel;
29
import javax.swing.JPopupMenu;
30
import javax.swing.JTree;
31
import javax.swing.event.TreeSelectionEvent;
32
import javax.swing.event.TreeSelectionListener;
33
import javax.swing.tree.DefaultMutableTreeNode;
34
import javax.swing.tree.DefaultTreeModel;
35
import javax.swing.tree.TreeNode;
36
import javax.swing.tree.TreePath;
37

    
38
import es.unex.sextante.additionalInfo.AdditionalInfoVectorLayer;
39
import es.unex.sextante.core.GeoAlgorithm;
40
import es.unex.sextante.core.IGeoAlgorithmFilter;
41
import es.unex.sextante.core.ParametersSet;
42
import es.unex.sextante.core.Sextante;
43
import es.unex.sextante.exceptions.NullParameterAdditionalInfoException;
44
import es.unex.sextante.gui.core.GeoAlgorithmExecutors;
45
import es.unex.sextante.gui.core.IGUIFactory;
46
import es.unex.sextante.gui.core.IToolboxRightButtonAction;
47
import es.unex.sextante.gui.core.NameAndIcon;
48
import es.unex.sextante.gui.core.SextanteGUI;
49
import es.unex.sextante.gui.core.ToolboxAction;
50
import es.unex.sextante.gui.help.HelpIO;
51
import es.unex.sextante.gui.history.History;
52
import es.unex.sextante.gui.settings.SextanteGeneralSettings;
53
import es.unex.sextante.parameters.Parameter;
54
import es.unex.sextante.parameters.ParameterVectorLayer;
55
import org.gvsig.tools.ToolsLocator;
56
import org.gvsig.tools.identitymanagement.SimpleIdentity;
57
import org.gvsig.tools.identitymanagement.SimpleIdentityManager;
58
import org.gvsig.tools.identitymanagement.UnauthorizedException;
59

    
60

    
61
/**
62
 * A panel showing the list of available algorithms, from which they can be executed
63
 * 
64
 * @author volaya
65
 * 
66
 */
67
public class AlgorithmsPanel
68
         extends
69
            JPanel {
70
   private static final int       EXECUTE_ITERATIVE_MENU_COUNT = 20;
71
   public static final String ACCESS_GEOPROCESS_AUTHORIZATION = "access-geoprocess";
72

    
73
   public static Font             TREE_FONT                    = new Font("Tahoma", Font.BOLD, 11);
74

    
75
   protected JTree                jTree;
76
   private TransparentScrollPane  jScrollPane;
77
   private JMenuItem              menuItemExecute;
78
   private JMenuItem              menuItemExecuteAsBatch;
79
   private JMenuItem              menuItemExecuteAsBatchFromGIS;
80
   private JMenuItem              menuItemExecuteIterative[];
81
   private JMenuItem              menuItemShowHelp;
82
   private JCheckBoxMenuItem      menuItemShowOnlyActive;
83
   private JPopupMenu             popupMenu;
84
   private TreePath               m_Path;
85
   private GeoAlgorithm           m_Alg;
86
   protected final IToolboxDialog m_ParentDialog;
87
   protected IGeoAlgorithmFilter  m_Filter;
88
   private final ImageIcon        m_BackgroundImg;
89
   protected String               m_sLastSearchString          = "";
90
   private int                    m_iExecuteIterativeMenuCount;
91

    
92
   protected boolean              m_bLastSearchIncludedHelpFiles;
93

    
94
   private ToolboxAction          m_Action;
95

    
96
   private JMenuItem[]            menuItemToolboxAction;
97

    
98
   private JMenuItem              menuItemSetOutputRendering;
99

    
100

    
101
   /**
102
    * Constructor
103
    * 
104
    * @param parentDialog
105
    *                the parent dialog. It will be the parent dialog of parameters dialog opened from this panel
106
    * @param filter
107
    *                the filter to apply to the list of all available algorithms
108
    */
109
   public AlgorithmsPanel(final IToolboxDialog parentDialog,
110
                          final IGeoAlgorithmFilter filter,
111
                          final ImageIcon img) {
112

    
113
      m_BackgroundImg = img;
114
      m_ParentDialog = parentDialog;
115
      m_Filter = filter;
116

    
117
      if (m_Filter == null) {
118
         m_Filter = new IGeoAlgorithmFilter() {
119
            public boolean accept(final GeoAlgorithm alg) {
120
               if (SextanteGUI.getShowOnlyActiveAlgorithms()) {
121
                  final Object[] objs = SextanteGUI.getInputFactory().getDataObjects();
122
                  return alg.meetsDataRequirements(objs);
123
               }
124
               else {
125
                  return true;
126
               }
127
            }
128
         };
129
      }
130

    
131
      init();
132

    
133
   }
134

    
135

    
136
   private void updateSelectedAlgorithm() {
137

    
138
      m_Alg = null;
139
      m_Action = null;
140

    
141
      if (m_Path != null) {
142
         menuItemExecuteAsBatch.setVisible(false);
143
         menuItemExecuteAsBatchFromGIS.setVisible(false);
144
         menuItemExecute.setVisible(false);
145
         menuItemShowHelp.setVisible(false);
146
         for (int i = 0; i < menuItemExecuteIterative.length; i++) {
147
            menuItemExecuteIterative[i].setVisible(false);
148
         }
149
         final DefaultMutableTreeNode node = (DefaultMutableTreeNode) m_Path.getLastPathComponent();
150
         if (!(node.getUserObject() instanceof GeoAlgorithm)) {
151
            if ((node.getUserObject() instanceof ToolboxAction)) {
152
               m_Action = (ToolboxAction) node.getUserObject();
153
            }
154
            return;
155
         }
156
         m_Alg = ((GeoAlgorithm) node.getUserObject());
157
         menuItemExecuteAsBatch.setVisible(true);
158
         menuItemShowHelp.setVisible(true);
159
         menuItemExecute.setVisible(true);
160
         final Object[] objs = SextanteGUI.getInputFactory().getDataObjects();
161
         menuItemExecuteAsBatchFromGIS.setVisible(true);
162
         final boolean bMeets = m_Alg.meetsDataRequirements(objs);
163
         menuItemExecuteAsBatchFromGIS.setEnabled(bMeets);
164
         menuItemExecute.setEnabled(bMeets);
165

    
166
         final IToolboxRightButtonAction[] actions = SextanteGUI.getToolboxRightButtonActions();
167
         for (int i = 0; i < menuItemToolboxAction.length; i++) {
168
            menuItemToolboxAction[i].setVisible(actions[i].canBeExecutedOnAlgorithm(m_Alg));
169
         }
170

    
171
         if (bMeets && m_Alg.requiresIndividualVectorLayers()) {
172
            m_iExecuteIterativeMenuCount = 0;
173
            final int iParams = m_Alg.getParameters().getNumberOfParameters();
174
            for (int i = 0; i < iParams; i++) {
175
               final Parameter param = m_Alg.getParameters().getParameter(i);
176
               if (param instanceof ParameterVectorLayer) {
177
                  final ParameterVectorLayer pvl = (ParameterVectorLayer) param;
178
                  boolean bMandatory;
179
                  try {
180
                     bMandatory = ((AdditionalInfoVectorLayer) pvl.getParameterAdditionalInfo()).getIsMandatory();
181
                     if (bMandatory) {
182
                        menuItemExecuteIterative[m_iExecuteIterativeMenuCount].setText(Sextante.getText("Run_iterative") + " ("
183
                                                                                       + param.getParameterDescription() + ")");
184
                        menuItemExecuteIterative[m_iExecuteIterativeMenuCount].setVisible(true);
185
                        m_iExecuteIterativeMenuCount++;
186
                     }
187
                  }
188
                  catch (final NullParameterAdditionalInfoException e) {
189
                  }
190
               }
191
            }
192
         }
193
      }
194

    
195
   }
196

    
197

    
198
   private void init() {
199

    
200
      m_iExecuteIterativeMenuCount = 0;
201
      this.setPreferredSize(new java.awt.Dimension(350, 380));
202
      this.setSize(new java.awt.Dimension(350, 380));
203
      final BorderLayout thisLayout = new BorderLayout();
204
      this.setLayout(thisLayout);
205
      jTree = new JTree();
206
      jTree.setOpaque(false);
207
      jTree.setCellRenderer(new AlgorithmTreeCellRenderer());
208
      final MouseListener ml = new MouseAdapter() {
209
         @Override
210
         public void mousePressed(MouseEvent e) {
211
            m_Alg = null;
212
            m_Action = null;
213
            menuItemExecute.setVisible(false);
214
            menuItemExecuteAsBatch.setVisible(false);
215
            for (int i = 0; i < EXECUTE_ITERATIVE_MENU_COUNT; i++) {
216
               menuItemExecuteIterative[i].setVisible(false);
217
            }
218
            for (int j = 0; j < menuItemToolboxAction.length; j++) {
219
               menuItemToolboxAction[j].setVisible(false);
220
            }
221
            menuItemExecuteAsBatchFromGIS.setVisible(false);
222
            menuItemShowHelp.setVisible(false);
223
            m_Path = jTree.getPathForLocation(e.getX(), e.getY());
224
            updateSelectedAlgorithm();
225

    
226
            // Create again DataObjects here to get feature selections changes (if there are)
227
            SextanteGUI.getInputFactory().createDataObjects();
228

    
229
            if (e.getButton() == MouseEvent.BUTTON1) {
230
               if (e.getClickCount() == 2) {
231
                  executeSelectedAlgorithm();
232
               }
233
            }
234
            else if (e.getButton() == MouseEvent.BUTTON3) {
235
               showPopupMenu(e);
236
            }
237
         }
238
      };
239
      jTree.addMouseListener(ml);
240
      jTree.addKeyListener(new KeyListener() {
241
         public void keyPressed(final KeyEvent e) {
242
         }
243

    
244

    
245
         public void keyReleased(final KeyEvent e) {
246
         }
247

    
248

    
249
         public void keyTyped(final KeyEvent e) {
250
            if (e.getKeyChar() == KeyEvent.VK_ENTER) {
251
               executeSelectedAlgorithm();
252
            }
253

    
254
            if (e.getKeyChar() == KeyEvent.VK_SPACE) {
255
               showPopupMenu(e);
256
            }
257
         }
258
      });
259

    
260
      jTree.addTreeSelectionListener(new TreeSelectionListener() {
261

    
262
         public void valueChanged(final TreeSelectionEvent e) {
263
            m_Path = e.getPath();
264
            updateSelectedAlgorithm();
265
         }
266

    
267
      });
268

    
269
      jScrollPane = new TransparentScrollPane(jTree);
270
      jScrollPane.setSize(new java.awt.Dimension(350, 380));
271
      if (m_BackgroundImg != null) {
272
         jScrollPane.setBackgroundImage(m_BackgroundImg);
273
      }
274
      this.add(jScrollPane, BorderLayout.CENTER);
275

    
276
      popupMenu = new JPopupMenu("Menu");
277

    
278
      menuItemExecute = new JMenuItem(Sextante.getText("Run"));
279
      menuItemExecute.addActionListener(new ActionListener() {
280
         public void actionPerformed(final ActionEvent evt) {
281
            executeSelectedAlgorithm();
282
         }
283
      });
284
      popupMenu.add(menuItemExecute);
285

    
286
      menuItemExecuteIterative = new JMenuItem[EXECUTE_ITERATIVE_MENU_COUNT];
287
      for (int i = 0; i < menuItemExecuteIterative.length; i++) {
288
         final int iParameter = i;
289
         menuItemExecuteIterative[i] = new JMenuItem();
290
         menuItemExecuteIterative[i].addActionListener(new ActionListener() {
291
            public void actionPerformed(final ActionEvent evt) {
292
               executeSelectedAlgorithmIteratively(iParameter);
293
            }
294
         });
295
         popupMenu.add(menuItemExecuteIterative[i]);
296
      }
297

    
298
      final IToolboxRightButtonAction[] actions = SextanteGUI.getToolboxRightButtonActions();
299
      menuItemToolboxAction = new JMenuItem[actions.length];
300
      for (int i = 0; i < actions.length; i++) {
301
         final IToolboxRightButtonAction action = actions[i];
302
         menuItemToolboxAction[i] = new JMenuItem();
303
         menuItemToolboxAction[i].setText(action.getDescription());
304
         menuItemToolboxAction[i].addActionListener(new ActionListener() {
305
            public void actionPerformed(final ActionEvent evt) {
306
               action.execute(m_Alg);
307
            }
308
         });
309
         popupMenu.add(menuItemToolboxAction[i]);
310
      }
311

    
312

    
313
      menuItemExecuteAsBatch = new JMenuItem(Sextante.getText("Execute_as_batch_process"));
314
      menuItemExecuteAsBatch.addActionListener(new ActionListener() {
315
         public void actionPerformed(final ActionEvent evt) {
316
            executeAsBatch();
317
         }
318
      });
319
      popupMenu.add(menuItemExecuteAsBatch);
320

    
321
      menuItemExecuteAsBatchFromGIS = new JMenuItem(Sextante.getText("Execute_as_batch_process__using_layers_from_GIS_app"));
322
      menuItemExecuteAsBatchFromGIS.addActionListener(new ActionListener() {
323
         public void actionPerformed(final ActionEvent evt) {
324
            executeAsBatchInGIS();
325
         }
326
      });
327
      popupMenu.add(menuItemExecuteAsBatchFromGIS);
328
      popupMenu.addSeparator();
329

    
330
      menuItemSetOutputRendering = new JMenuItem(Sextante.getText("set_output_rendering"));
331
      menuItemSetOutputRendering.addActionListener(new ActionListener() {
332
         public void actionPerformed(final ActionEvent evt) {
333
            setOutputRendering();
334
         }
335
      });
336
      popupMenu.add(menuItemSetOutputRendering);
337
      popupMenu.addSeparator();
338

    
339
      final JMenuItem menuItemExpand = new JMenuItem(Sextante.getText("Expand_all"));
340
      menuItemExpand.addActionListener(new ActionListener() {
341
         public void actionPerformed(final ActionEvent evt) {
342
            expandAll();
343
         }
344
      });
345
      popupMenu.add(menuItemExpand);
346

    
347
      final JMenuItem menuItemCollapse = new JMenuItem(Sextante.getText("Collapse_all"));
348
      menuItemCollapse.addActionListener(new ActionListener() {
349
         public void actionPerformed(final ActionEvent evt) {
350
            collapseAll();
351
         }
352
      });
353
      popupMenu.add(menuItemCollapse);
354

    
355
      menuItemShowOnlyActive = new JCheckBoxMenuItem(Sextante.getText("Show_active_only"));
356
      menuItemShowOnlyActive.addItemListener(new ItemListener() {
357
         public void itemStateChanged(final ItemEvent e) {
358
            SextanteGUI.setShowOnlyActiveAlgorithms(e.getStateChange() == ItemEvent.SELECTED);
359
            fillTree(m_sLastSearchString, m_bLastSearchIncludedHelpFiles);
360
            collapseAll();
361
         }
362
      });
363
      menuItemShowOnlyActive.setSelected(SextanteGUI.getShowOnlyActiveAlgorithms());
364
      popupMenu.add(menuItemShowOnlyActive);
365

    
366
      popupMenu.addSeparator();
367

    
368
      menuItemShowHelp = new JMenuItem(Sextante.getText("Show_help"));
369
      menuItemShowHelp.addActionListener(new ActionListener() {
370
         public void actionPerformed(final ActionEvent evt) {
371
            showHelp();
372
         }
373
      });
374
      popupMenu.add(menuItemShowHelp);
375

    
376
   }
377

    
378

    
379
   protected void setOutputRendering() {
380

    
381
      if (m_Alg != null) {
382
         final OutputRenderingSettingsDialog dialog = new OutputRenderingSettingsDialog(m_Alg);
383
         dialog.setVisible(true);
384
         final HashMap<String, Object> set = dialog.getSettings();
385
         if (set != null) {
386
            SextanteGUI.getDataRenderer().setRenderingForAlgorithm(m_Alg.getCommandLineName(), set);
387
            SextanteGUI.getDataRenderer().save();
388
         }
389
      }
390

    
391
   }
392

    
393

    
394
   protected void showHelp() {
395

    
396
      if (m_Alg != null) {
397
         SextanteGUI.getGUIFactory().showHelpDialog(m_Alg);
398
      }
399

    
400
   }
401

    
402

    
403
   protected void showPopupMenu(final MouseEvent e) {
404

    
405
      jTree.setSelectionPath(m_Path);
406
      popupMenu.show(e.getComponent(), e.getX(), e.getY());
407

    
408
   }
409

    
410

    
411
   protected void showPopupMenu(final KeyEvent e) {
412

    
413
      jTree.setSelectionPath(m_Path);
414
      final Rectangle pathBounds = jTree.getPathBounds(m_Path);
415
      popupMenu.show(e.getComponent(), pathBounds.x, pathBounds.y);
416

    
417
   }
418

    
419

    
420
   /**
421
    * Collapses the tree of algorithms
422
    */
423
   public void collapseAll() {
424

    
425
      final TreeNode root = (TreeNode) jTree.getModel().getRoot();
426
      final TreePath path = new TreePath(root);
427
      expandAll(jTree, path, false);
428
      jTree.expandPath(path);
429
      final int iChildCount = root.getChildCount();
430
      for (int i = 0; i < iChildCount; i++) {
431
         final TreeNode node = root.getChildAt(i);
432
         final TreePath subpath = path.pathByAddingChild(node);
433
         jTree.expandPath(subpath);
434
      }
435

    
436
   }
437

    
438

    
439
   /**
440
    * Expands the tree of algorithms
441
    */
442
   public void expandAll() {
443

    
444
      final TreeNode root = (TreeNode) jTree.getModel().getRoot();
445
      expandAll(jTree, new TreePath(root), true);
446

    
447
   }
448

    
449

    
450
   private void expandAll(final JTree tree,
451
                          final TreePath parent,
452
                          final boolean expand) {
453

    
454
      final TreeNode node = (TreeNode) parent.getLastPathComponent();
455
      if (node.getChildCount() >= 0) {
456
         for (final Enumeration e = node.children(); e.hasMoreElements();) {
457
            final TreeNode n = (TreeNode) e.nextElement();
458
            final TreePath path = parent.pathByAddingChild(n);
459
            expandAll(tree, path, expand);
460
         }
461
      }
462

    
463
      if (expand) {
464
         tree.expandPath(parent);
465
      }
466
      else {
467
         tree.collapsePath(parent);
468
      }
469

    
470
   }
471

    
472

    
473
   protected void executeAsBatch() {
474

    
475
      if (m_Alg != null) {
476
         SextanteGUI.getGUIFactory().showBatchProcessingDialog(m_Alg, m_ParentDialog.getDialog());
477
      }
478

    
479
   }
480

    
481

    
482
   protected void executeAsBatchInGIS() {
483

    
484
      if (m_Alg != null) {
485
         SextanteGUI.getGUIFactory().showBatchProcessingFromGISDialog(m_Alg, m_ParentDialog.getDialog());
486
      }
487

    
488
   }
489

    
490

    
491
   protected void executeSelectedAlgorithm() {
492

    
493
      try {
494
         if (m_Alg != null) {
495
            final GeoAlgorithm alg = m_Alg.getNewInstance();
496
            final int iRet = SextanteGUI.getGUIFactory().showAlgorithmDialog(alg, m_ParentDialog.getDialog(), null);
497
            if (iRet == IGUIFactory.OK) {
498
               final String[] cmd = alg.getAlgorithmAsCommandLineSentences();
499
               if (cmd != null) {
500
                  History.addToHistory(cmd);
501
               }
502
               GeoAlgorithmExecutors.execute(alg, m_ParentDialog.getDialog());
503
               updateListOfMostRecentAlgorithms();
504
            }
505
         }
506
         else if (m_Action != null) {
507
            m_Action.execute();
508
         }
509
      }
510
      catch (final Exception e) {
511
         Sextante.addErrorToLog(e);
512
      }
513

    
514
   }
515

    
516

    
517
   private void updateListOfMostRecentAlgorithms() {
518

    
519
      final boolean bShowMostRecent = new Boolean(SextanteGUI.getSettingParameterValue(SextanteGeneralSettings.SHOW_MOST_RECENT)).booleanValue();
520
      if (bShowMostRecent) {
521
         final GeoAlgorithm[] recent = History.getRecentlyUsedAlgs();
522
         final DefaultMutableTreeNode recentNode = new DefaultMutableTreeNode(Sextante.getText("RecentAlgorithms"));
523
         for (int i = 0; i < recent.length; i++) {
524
            final DefaultMutableTreeNode node = new DefaultMutableTreeNode(recent[i]);
525
            recentNode.add(node);
526
         }
527
         final DefaultMutableTreeNode mainNode = (DefaultMutableTreeNode) new TreePath(jTree.getModel().getRoot()).getLastPathComponent();
528
         mainNode.remove(0);
529
         mainNode.insert(recentNode, 0);
530
      }
531

    
532
   }
533

    
534

    
535
   protected void executeSelectedAlgorithmIteratively(final int iParameterToIterateOver) {
536

    
537
      try {
538
         if (m_Alg != null) {
539
            final GeoAlgorithm alg = m_Alg.getNewInstance();
540
            final int iRet = SextanteGUI.getGUIFactory().showAlgorithmDialog(alg, m_ParentDialog.getDialog(), null);
541
            if (iRet == IGUIFactory.OK) {
542
               final ParametersSet params = m_Alg.getParameters();
543
               final int iParamCount = params.getNumberOfParameters();
544
               int iVectorLayers = 0;
545
               for (int i = 0; i < iParamCount; i++) {
546
                  final Parameter param = m_Alg.getParameters().getParameter(i);
547
                  if (param instanceof ParameterVectorLayer) {
548
                     if (iVectorLayers == iParameterToIterateOver) {
549
                        GeoAlgorithmExecutors.executeIterative(alg, m_ParentDialog.getDialog(), param.getParameterName());
550
                        break;
551
                     }
552
                     iVectorLayers++;
553
                  }
554
               }
555
            }
556
         }
557
      }
558
      catch (final Exception e) {
559
         Sextante.addErrorToLog(e);
560
      }
561

    
562
   }
563

    
564

    
565
   public GeoAlgorithm getSelectedAlgorithm() {
566

    
567
      return m_Alg;
568

    
569
   }
570

    
571

    
572
   /**
573
    * Fills the tree with the algorithms that match a search criteria
574
    * 
575
    * @param sSearchString
576
    *                The search string to look for in the algorithms context help
577
    * @param bSearchInFiles
578
    *                true if it should search in help files. if false, it will only search in algorithm names
579
    * @return the number of algorithms that match the given criteria
580
    */
581
   public int fillTree(final String sSearchString,
582
                       final boolean bSearchInHelpFiles) {
583

    
584
      m_sLastSearchString = sSearchString;
585
      m_bLastSearchIncludedHelpFiles = bSearchInHelpFiles;
586

    
587
      SimpleIdentity user = ToolsLocator.getIdentityManager().getCurrentIdentity();
588

    
589
      int iCount = 0;
590
      String sGroup, sSubgroup;
591
      final DefaultMutableTreeNode mainNode = new DefaultMutableTreeNode(Sextante.getText("Algorithms"));
592
      final HashMap<Object, HashMap<String, HashMap<String, DefaultMutableTreeNode>>> groups = new HashMap<Object, HashMap<String, HashMap<String, DefaultMutableTreeNode>>>();
593

    
594
      setCursor(new Cursor(Cursor.WAIT_CURSOR));
595
      SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
596

    
597
      //algorithms
598
      final HashMap<String, HashMap<String, GeoAlgorithm>> algs = Sextante.getAlgorithms();
599
      final Set<String> groupKeys = algs.keySet();
600
      final Iterator<String> groupIter = groupKeys.iterator();
601
      while (groupIter.hasNext()) {
602
         final String groupKey = groupIter.next();
603
         final HashMap<String, GeoAlgorithm> groupAlgs = algs.get(groupKey);
604
         final Set keys = groupAlgs.keySet();
605
         final Iterator iter = keys.iterator();
606
         while (iter.hasNext()) {
607
            final GeoAlgorithm alg = groupAlgs.get(iter.next());
608
            if( ! identityManager.getCurrentIdentity().isAuthorized(ACCESS_GEOPROCESS_AUTHORIZATION, alg, alg.getCommandLineName()) ) {
609
                continue;
610
            }            
611
            if (m_Filter.accept(alg)) {
612
               if (bSearchInHelpFiles) {
613
                  if (!HelpIO.containsStringInHelpFile(alg, sSearchString)) {
614
                     continue;
615
                  }
616
               }
617
               else {
618
                  if ((sSearchString != null) && !alg.getName().toLowerCase().contains(sSearchString)) {
619
                     continue;
620
                  }
621
               }
622
               iCount++;
623
               final AlgorithmGroupConfiguration conf = AlgorithmGroupsOrganizer.getGroupConfiguration(alg);
624
               if (conf != null) {
625
                  if (!conf.isShow()) {
626
                     continue;
627
                  }
628
                  sGroup = conf.getGroup();
629
                  sSubgroup = conf.getSubgroup();
630
               }
631
               else {
632
                  sGroup = groupKey;
633
                  sSubgroup = alg.getGroup();
634
               }
635
               HashMap<String, HashMap<String, DefaultMutableTreeNode>> group = groups.get(sGroup);
636
               if (group == null) {
637
                  group = new HashMap<String, HashMap<String, DefaultMutableTreeNode>>();
638
                  groups.put(sGroup, group);
639
               }
640
               HashMap<String, DefaultMutableTreeNode> subgroup = group.get(sSubgroup);
641
               if (subgroup == null) {
642
                  subgroup = new HashMap<String, DefaultMutableTreeNode>();
643
                  group.put(sSubgroup, subgroup);
644
               }
645
               subgroup.put(alg.getName(), new DefaultMutableTreeNode(alg));
646
            }
647
         }
648
      }
649

    
650
      //toolbox actions
651
      final HashMap<NameAndIcon, ArrayList<ToolboxAction>> allActions = SextanteGUI.getToolboxActions();
652
      final Set<NameAndIcon> actionsKeys = allActions.keySet();
653
      final Iterator<NameAndIcon> actionsIter = actionsKeys.iterator();
654
      while (actionsIter.hasNext()) {
655
         final NameAndIcon nai = actionsIter.next();
656
         final ArrayList<ToolboxAction> actions = allActions.get(nai);
657
         for (int i = 0; i < actions.size(); i++) {
658
            final ToolboxAction ita = actions.get(i);
659
            if( !user.isAuthorized(ACCESS_GEOPROCESS_AUTHORIZATION, ita, ita.getName()) ) {
660
                continue;
661
            }
662
            if ((sSearchString != null) && !ita.getName().toLowerCase().contains(sSearchString)) {
663
               continue;
664
            }
665
            iCount++;
666
            sSubgroup = ita.getGroup();
667
            HashMap<String, HashMap<String, DefaultMutableTreeNode>> group = groups.get(nai.getName());
668
            if (group == null) {
669
               group = groups.get(nai);
670
            }
671
            if (group == null) {
672
               group = new HashMap<String, HashMap<String, DefaultMutableTreeNode>>();
673
               groups.put(nai, group);
674
            }
675
            HashMap<String, DefaultMutableTreeNode> subgroup = group.get(sSubgroup);
676
            if (subgroup == null) {
677
               subgroup = new HashMap<String, DefaultMutableTreeNode>();
678
               group.put(sSubgroup, subgroup);
679
            }
680
            subgroup.put(ita.getName(), new DefaultMutableTreeNode(ita));
681

    
682
         }
683
      }
684

    
685

    
686
      final Set<Object> set = groups.keySet();
687
      final Iterator<Object> iter = set.iterator();
688
      while (iter.hasNext()) {
689
         final Object key = iter.next();
690
         final DefaultMutableTreeNode node = new DefaultMutableTreeNode(key);
691
         addNodeInSortedOrder(mainNode, node);
692
         final HashMap<String, HashMap<String, DefaultMutableTreeNode>> g = groups.get(key);
693
         final Set<String> set2 = g.keySet();
694
         final Iterator<String> iter2 = set2.iterator();
695
         while (iter2.hasNext()) {
696
            final String sKey2 = iter2.next();
697
            final DefaultMutableTreeNode node2 = new DefaultMutableTreeNode(sKey2);
698
            addNodeInSortedOrder(node, node2);
699
            final HashMap<String, DefaultMutableTreeNode> g2 = g.get(sKey2);
700
            final Set<String> set3 = g2.keySet();
701
            final Iterator<String> iter3 = set3.iterator();
702
            while (iter3.hasNext()) {
703
               final String sKey3 = iter3.next();
704
               final DefaultMutableTreeNode node3 = g2.get(sKey3);
705
               addNodeInSortedOrder(node2, node3);
706
            }
707
         }
708
      }
709

    
710
      final boolean bShowMostRecent = new Boolean(SextanteGUI.getSettingParameterValue(SextanteGeneralSettings.SHOW_MOST_RECENT)).booleanValue();
711
      if (bShowMostRecent) {
712
         final GeoAlgorithm[] recent = History.getRecentlyUsedAlgs();
713
         final DefaultMutableTreeNode recentNode = new DefaultMutableTreeNode(Sextante.getText("RecentAlgorithms"));
714
         for (int i = 0; i < recent.length; i++) {
715
            final DefaultMutableTreeNode node = new DefaultMutableTreeNode(recent[i]);
716
            recentNode.add(node);
717
         }
718
         mainNode.insert(recentNode, 0);
719
      }
720

    
721
      setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
722

    
723
      jTree.setModel(new DefaultTreeModel(mainNode));
724

    
725
      if (sSearchString != null) {
726
         expandAll();
727
      }
728

    
729
      m_ParentDialog.setAlgorithmsCount(iCount);
730

    
731
      return iCount;
732

    
733
   }
734

    
735

    
736
   private void addNodeInSortedOrder(final DefaultMutableTreeNode parent,
737
                                     final DefaultMutableTreeNode child) {
738

    
739
      final int n = parent.getChildCount();
740
      if (n == 0) {
741
         parent.add(child);
742
         return;
743
      }
744
      final Collator collator = Collator.getInstance();
745
      collator.setStrength(Collator.PRIMARY);
746
      DefaultMutableTreeNode node = null;
747
      for (int i = 0; i < n; i++) {
748
         node = (DefaultMutableTreeNode) parent.getChildAt(i);
749
         try {
750
            if (collator.compare(node.toString(), child.toString()) > 0) {
751
               parent.insert(child, i);
752
               return;
753
            }
754
         }
755
         catch (final Exception e) {
756
         }
757
      }
758
      parent.add(child);
759

    
760
   }
761

    
762
}