~motu-torrent/azureus/upstream-stable

« back to all changes in this revision

Viewing changes to org/gudy/azureus2/ui/swt/views/ConfigView.java

  • Committer: John Dong
  • Date: 2007-10-22 04:54:13 UTC
  • Revision ID: john.dong@gmail.com-20071022045413-3ovb11u82rrcokxx
Commit 3.0.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Created on 2 juil. 2003
3
 
 * Copyright (C) 2003, 2004, 2005, 2006 Aelitis, All Rights Reserved.
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU General Public License
7
 
 * as published by the Free Software Foundation; either version 2
8
 
 * of the License, or (at your option) any later version.
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program; if not, write to the Free Software
15
 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 
 *
17
 
 * AELITIS, SAS au capital de 46,603.30 euros
18
 
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
19
 
 *
20
 
 */
21
 
package org.gudy.azureus2.ui.swt.views;
22
 
 
23
 
import java.util.ArrayList;
24
 
import java.util.Arrays;
25
 
import java.util.Comparator;
26
 
 
27
 
import org.eclipse.swt.SWT;
28
 
import org.eclipse.swt.custom.SashForm;
29
 
import org.eclipse.swt.custom.ScrolledComposite;
30
 
import org.eclipse.swt.custom.StackLayout;
31
 
import org.eclipse.swt.events.*;
32
 
import org.eclipse.swt.graphics.Font;
33
 
import org.eclipse.swt.graphics.FontData;
34
 
import org.eclipse.swt.graphics.Image;
35
 
import org.eclipse.swt.layout.*;
36
 
import org.eclipse.swt.widgets.*;
37
 
 
38
 
import org.gudy.azureus2.core3.config.COConfigurationManager;
39
 
import org.gudy.azureus2.core3.internat.MessageText;
40
 
import org.gudy.azureus2.core3.logging.LogEvent;
41
 
import org.gudy.azureus2.core3.logging.LogIDs;
42
 
import org.gudy.azureus2.core3.logging.Logger;
43
 
import org.gudy.azureus2.core3.util.*;
44
 
import org.gudy.azureus2.pluginsimpl.local.ui.config.ConfigSectionRepository;
45
 
import org.gudy.azureus2.ui.swt.ImageRepository;
46
 
import org.gudy.azureus2.ui.swt.Messages;
47
 
import org.gudy.azureus2.ui.swt.Utils;
48
 
import org.gudy.azureus2.ui.swt.plugins.UISWTConfigSection;
49
 
import org.gudy.azureus2.ui.swt.views.configsections.*;
50
 
 
51
 
import com.aelitis.azureus.core.AzureusCore;
52
 
 
53
 
import org.gudy.azureus2.plugins.ui.config.ConfigSection;
54
 
import org.gudy.azureus2.plugins.ui.config.ConfigSectionSWT;
55
 
 
56
 
/**
57
 
 * @author Olivier
58
 
 *
59
 
 */
60
 
public class ConfigView extends AbstractIView {
61
 
        private static final LogIDs LOGID = LogIDs.GUI;
62
 
  public static final String sSectionPrefix = "ConfigView.section.";
63
 
  
64
 
  AzureusCore           azureus_core;
65
 
  
66
 
  Composite cConfig;
67
 
  Composite cConfigSection;
68
 
  StackLayout layoutConfigSection;
69
 
  Label lHeader;
70
 
  Font headerFont;
71
 
  Font filterFoundFont;
72
 
  Tree tree;
73
 
  ArrayList pluginSections;
74
 
 
75
 
        private Timer filterDelayTimer;
76
 
        private String filterText = "";
77
 
        private Label lblX;
78
 
 
79
 
  /**
80
 
   * Main Initializer
81
 
   * 
82
 
   * @param _azureus_core
83
 
   */
84
 
  public 
85
 
  ConfigView(
86
 
        AzureusCore             _azureus_core ) 
87
 
  {
88
 
        azureus_core    = _azureus_core;
89
 
  }
90
 
 
91
 
  /* (non-Javadoc)
92
 
   * @see org.gudy.azureus2.ui.swt.IView#initialize(org.eclipse.swt.widgets.Composite)
93
 
   */
94
 
  public void initialize(Composite composite) {
95
 
    GridData gridData;
96
 
    /*
97
 
    /--cConfig-----------------------------------------------------------\
98
 
    | ###SashForm#form################################################## |
99
 
    | # /--cLeftSide-\ /--cRightSide---------------------------------\ # |
100
 
    | # |txtFilter X | | ***cHeader********************************* | # |
101
 
    | # | ##tree#### | | * lHeader                                 * | # |
102
 
    | # | #        # | | ******************************************* | # |
103
 
    | # | #        # | | ###Composite cConfigSection################ | # |
104
 
    | # | #        # | | #                                         # | # |
105
 
    | # | #        # | | #                                         # | # |
106
 
    | # | #        # | | #                                         # | # |
107
 
    | # | #        # | | #                                         # | # |
108
 
    | # | ########## | | ########################################### | # |
109
 
    | # \------------/ \---------------------------------------------/ # |
110
 
    | ################################################################## |
111
 
    |  [Button]                                                          |
112
 
    \--------------------------------------------------------------------/
113
 
    */
114
 
    try {
115
 
      Display d = composite.getDisplay();
116
 
 
117
 
      cConfig = new Composite(composite, SWT.NONE);
118
 
      GridLayout configLayout = new GridLayout();
119
 
      configLayout.marginHeight = 0;
120
 
      configLayout.marginWidth = 0;
121
 
      cConfig.setLayout(configLayout);
122
 
      gridData = new GridData(GridData.FILL_BOTH);
123
 
      cConfig.setLayoutData(gridData);
124
 
  
125
 
      SashForm form = new SashForm(cConfig,SWT.HORIZONTAL);
126
 
      gridData = new GridData(GridData.FILL_BOTH);
127
 
      form.setLayoutData(gridData);
128
 
      
129
 
      Composite cLeftSide = new Composite(form, SWT.BORDER);
130
 
      gridData = new GridData(GridData.FILL_BOTH);
131
 
      cLeftSide.setLayoutData(gridData);
132
 
      
133
 
      FormLayout layout = new FormLayout();
134
 
      cLeftSide.setLayout(layout);
135
 
      
136
 
      final Text txtFilter = new Text(cLeftSide, SWT.BORDER);
137
 
      final String sFilterText = MessageText.getString("ConfigView.filter");
138
 
      txtFilter.setText(sFilterText);
139
 
      txtFilter.selectAll();
140
 
      txtFilter.addModifyListener(new ModifyListener() {
141
 
        public void modifyText(ModifyEvent e) {
142
 
                filterTree(txtFilter.getText());
143
 
        }
144
 
      });
145
 
      txtFilter.addMouseListener(new MouseAdapter() {
146
 
                                public void mouseDown(MouseEvent e) {
147
 
                                        if (txtFilter.getText().equals(sFilterText)) {
148
 
                                                txtFilter.selectAll();
149
 
                                        }
150
 
                                }
151
 
                        });
152
 
      txtFilter.setFocus();
153
 
 
154
 
                lblX = new Label(cLeftSide, SWT.WRAP);
155
 
      Messages.setLanguageTooltip(lblX, "MyTorrentsView.clearFilter.tooltip");
156
 
      lblX.setImage(ImageRepository.getImage("smallx-gray"));
157
 
      lblX.addMouseListener(new MouseAdapter() {
158
 
        public void mouseUp(MouseEvent e) {
159
 
                txtFilter.setText("");
160
 
        }
161
 
      });
162
 
  
163
 
      tree = new Tree(cLeftSide, SWT.NONE);
164
 
      FontData[] fontData = tree.getFont().getFontData();
165
 
      fontData[0].setStyle(SWT.BOLD);
166
 
      filterFoundFont = new Font(d, fontData);
167
 
      
168
 
      cLeftSide.setBackground(tree.getBackground());
169
 
      lblX.setBackground(tree.getBackground());
170
 
 
171
 
      FormData formData;
172
 
      formData = new FormData();
173
 
      formData.top = new FormAttachment(0,5);
174
 
      formData.left = new FormAttachment(0,5);
175
 
      formData.right = new FormAttachment(lblX, -3);
176
 
      txtFilter.setLayoutData(formData);
177
 
 
178
 
      formData = new FormData();
179
 
      formData.top = new FormAttachment(0,5);
180
 
      formData.right = new FormAttachment(100,-5);
181
 
      lblX.setLayoutData(formData);
182
 
 
183
 
      formData = new FormData();
184
 
      formData.top = new FormAttachment(txtFilter,5);
185
 
      formData.left = new FormAttachment(0,0);
186
 
      formData.right = new FormAttachment(100,0);
187
 
      formData.bottom = new FormAttachment(100,0);
188
 
      tree.setLayoutData(formData);
189
 
  
190
 
      Composite cRightSide = new Composite(form, SWT.NULL);
191
 
      configLayout = new GridLayout();
192
 
      configLayout.marginHeight = 3;
193
 
      configLayout.marginWidth = 0;
194
 
      cRightSide.setLayout(configLayout);
195
 
  
196
 
      // Header
197
 
      Composite cHeader = new Composite(cRightSide, SWT.BORDER);
198
 
      configLayout = new GridLayout();
199
 
      configLayout.marginHeight = 3;
200
 
      configLayout.marginWidth = 0;
201
 
      cHeader.setLayout(configLayout);
202
 
      gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
203
 
      cHeader.setLayoutData(gridData);
204
 
  
205
 
      cHeader.setBackground(d.getSystemColor(SWT.COLOR_LIST_SELECTION));
206
 
      cHeader.setForeground(d.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
207
 
  
208
 
      lHeader = new Label(cHeader, SWT.NULL);
209
 
      lHeader.setBackground(d.getSystemColor(SWT.COLOR_LIST_SELECTION));
210
 
      lHeader.setForeground(d.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
211
 
      fontData = lHeader.getFont().getFontData();
212
 
      fontData[0].setStyle(SWT.BOLD);
213
 
      int fontHeight = (int)(fontData[0].getHeight() * 1.2);
214
 
      fontData[0].setHeight(fontHeight);
215
 
      headerFont = new Font(d, fontData);
216
 
      lHeader.setFont(headerFont);
217
 
      gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
218
 
      lHeader.setLayoutData(gridData);
219
 
  
220
 
      // Config Section
221
 
      cConfigSection = new Composite(cRightSide, SWT.NULL);
222
 
      layoutConfigSection = new StackLayout();
223
 
      cConfigSection.setLayout(layoutConfigSection);
224
 
      gridData = new GridData(GridData.FILL_BOTH);
225
 
      cConfigSection.setLayoutData(gridData);
226
 
  
227
 
  
228
 
      form.setWeights(new int[] {20,80});
229
 
  
230
 
      tree.addSelectionListener(new SelectionAdapter() {
231
 
        public void widgetSelected(SelectionEvent e) {
232
 
          Tree tree = (Tree)e.getSource();
233
 
          //Check that at least an item is selected
234
 
          //OSX lets you select nothing in the tree for example when a child is selected
235
 
          //and you close its parent.
236
 
          if(tree.getSelection().length > 0)
237
 
              showSection(tree.getSelection()[0]);
238
 
           }
239
 
      });
240
 
      // Double click = expand/contract branch
241
 
      tree.addListener(SWT.DefaultSelection, new Listener() {
242
 
        public void handleEvent(Event e) {
243
 
            TreeItem item = (TreeItem)e.item;
244
 
            if (item != null)
245
 
              item.setExpanded(!item.getExpanded());
246
 
        }
247
 
      });
248
 
    } catch (Exception e) {
249
 
        Logger.log(new LogEvent(LOGID, "Error initializing ConfigView", e));
250
 
    }
251
 
 
252
 
 
253
 
 
254
 
    // Add sections
255
 
    /** How to add a new section
256
 
     * 1) Create a new implementation of ConfigSectionSWT in a new file
257
 
     *    (Use the ConfigSectionTMP.java as a template if it's still around)
258
 
     * 2) import it into here
259
 
     * 3) add it to the internal sections list
260
 
     */
261
 
    pluginSections = ConfigSectionRepository.getInstance().getList();
262
 
 
263
 
    ConfigSection[] internalSections = { new ConfigSectionConnection(),
264
 
                                         new ConfigSectionConnectionProxy(),
265
 
                                         new ConfigSectionConnectionAdvanced(),
266
 
                                         new ConfigSectionConnectionEncryption(),
267
 
                                         new ConfigSectionTransfer(azureus_core),
268
 
                                         new ConfigSectionTransferAutoSpeed(),
269
 
                                         new ConfigSectionTransferLAN(),
270
 
                                         new ConfigSectionFile(), 
271
 
                                         new ConfigSectionFileMove(),
272
 
                                         new ConfigSectionFileTorrents(),
273
 
                                         new ConfigSectionFileTorrentsDecoding(),
274
 
                                         new ConfigSectionFilePerformance(),
275
 
                                         new ConfigSectionInterface(),
276
 
                                         new ConfigSectionInterfaceLanguage(),
277
 
                                         new ConfigSectionInterfaceStart(),
278
 
                                         new ConfigSectionInterfaceDisplay(),
279
 
                                         new ConfigSectionInterfaceColor(),
280
 
                                         new ConfigSectionInterfaceAlerts(),
281
 
                                         new ConfigSectionMode(),
282
 
                                         new ConfigSectionIPFilter(azureus_core),
283
 
                                         new ConfigSectionPlugins(this, azureus_core),
284
 
                                         new ConfigSectionStats(),
285
 
                                         new ConfigSectionTracker(azureus_core),
286
 
                                         new ConfigSectionTrackerClient(),
287
 
                                         new ConfigSectionTrackerServer(azureus_core),
288
 
                                         new ConfigSectionSecurity(),
289
 
                                         new ConfigSectionSharing(),
290
 
                                         new ConfigSectionLogging()
291
 
                                        };
292
 
    
293
 
    pluginSections.addAll(0, Arrays.asList(internalSections));
294
 
 
295
 
    for (int i = 0; i < pluginSections.size(); i++) {
296
 
   
297
 
        // slip the non-standard "plugins" initialisation inbetween the internal ones
298
 
        // and the plugin ones so plugin ones can be children of it
299
 
        
300
 
      boolean   plugin_section = i >= internalSections.length;
301
 
      
302
 
      ConfigSection section = (ConfigSection)pluginSections.get(i);
303
 
      
304
 
      if (section instanceof ConfigSectionSWT || section instanceof UISWTConfigSection ) {
305
 
        String name;
306
 
        try {
307
 
          name = section.configSectionGetName();
308
 
         } catch (Exception e) {
309
 
                 Logger.log(new LogEvent(LOGID, "A ConfigSection plugin caused an "
310
 
                                                        + "error while trying to call its "
311
 
                                                        + "configSectionGetName function", e));
312
 
          name = "Bad Plugin";
313
 
        }
314
 
 
315
 
         String section_key = name;
316
 
         
317
 
         if ( plugin_section ){
318
 
                        // if resource exists without prefix then use it as plugins don't
319
 
                        // need to start with the prefix
320
 
                
321
 
                if ( !MessageText.keyExists(section_key)){
322
 
                        
323
 
                        section_key = sSectionPrefix + name;
324
 
                }
325
 
                
326
 
         }else{
327
 
                
328
 
                section_key = sSectionPrefix + name;
329
 
         }
330
 
         
331
 
         try {
332
 
          TreeItem treeItem;
333
 
          String location = section.configSectionGetParentSection();
334
 
  
335
 
          if (location.equalsIgnoreCase(ConfigSection.SECTION_ROOT))
336
 
                  treeItem = new TreeItem(tree, SWT.NULL);
337
 
          else if (location != "") {
338
 
                  TreeItem treeItemFound = findTreeItem(tree, location);
339
 
                  if (treeItemFound != null){
340
 
                          if (location.equalsIgnoreCase(ConfigSection.SECTION_PLUGINS)) {
341
 
                                  // Force ordering by name here.
342
 
                                  int position = findInsertPointFor(MessageText.getString(section_key), treeItemFound);
343
 
                                  if (position == -1) {
344
 
                                          treeItem = new TreeItem(treeItemFound, SWT.NULL);
345
 
                                  }
346
 
                                  else {
347
 
                                          treeItem = new TreeItem(treeItemFound, SWT.NULL, position);
348
 
                                  }
349
 
                          }
350
 
                          else {
351
 
                                  treeItem = new TreeItem(treeItemFound, SWT.NULL);
352
 
                          }
353
 
                  }else{
354
 
                          treeItem = new TreeItem(tree, SWT.NULL);
355
 
                  }
356
 
          }else{
357
 
                  treeItem = new TreeItem(tree, SWT.NULL); 
358
 
          }
359
 
  
360
 
          ScrolledComposite sc = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
361
 
          sc.setExpandHorizontal(true);
362
 
          sc.setExpandVertical(true);
363
 
          sc.setLayoutData(new GridData(GridData.FILL_BOTH));
364
 
                sc.getVerticalBar().setIncrement(16);
365
 
                sc.addListener(SWT.Resize, new Listener() {
366
 
                        public void handleEvent(Event event) {
367
 
                                ScrolledComposite sc = (ScrolledComposite)event.widget;
368
 
                        sc.getVerticalBar().setPageIncrement(sc.getSize().y);
369
 
                        }
370
 
                });
371
 
          
372
 
          if(i == 0) {
373
 
            Composite c;
374
 
            if ( section instanceof ConfigSectionSWT ){
375
 
                  
376
 
                  c = ((ConfigSectionSWT)section).configSectionCreate(sc);
377
 
                  
378
 
            }else{
379
 
   
380
 
                  c = ((UISWTConfigSection)section).configSectionCreate(sc);
381
 
            }
382
 
            sc.setContent(c);
383
 
          }
384
 
          
385
 
          Messages.setLanguageText(treeItem, section_key);
386
 
          treeItem.setData("Panel", sc);
387
 
          treeItem.setData("ID", name);
388
 
          treeItem.setData("ConfigSectionSWT", section);
389
 
          
390
 
          // ConfigSectionPlugins is special because it has to handle the
391
 
          // PluginConfigModel config pages
392
 
          if (section instanceof ConfigSectionPlugins)
393
 
                ((ConfigSectionPlugins)section).initPluginSubSections();
394
 
        } catch (Exception e) {
395
 
                Logger.log(new LogEvent(LOGID, "ConfigSection plugin '" + name
396
 
                                                        + "' caused an error", e));
397
 
        }
398
 
      }
399
 
    }
400
 
    
401
 
 
402
 
 
403
 
 
404
 
    initSaveButton();
405
 
 
406
 
    TreeItem[] items = { tree.getItems()[0] };
407
 
    tree.setSelection(items);
408
 
    // setSelection doesn't trigger a SelectionListener, so..
409
 
    showSection(items[0]);
410
 
  }
411
 
 
412
 
  /**
413
 
         * @param text
414
 
         */
415
 
        protected void filterTree(String text) {
416
 
                filterText = text;
417
 
                if (filterDelayTimer != null) {
418
 
                        filterDelayTimer.destroy();
419
 
                }
420
 
                
421
 
                if (lblX != null && !lblX.isDisposed()) {
422
 
                        Image img = ImageRepository.getImage(filterText.length() > 0 ? "smallx"
423
 
                                        : "smallx-gray");
424
 
 
425
 
                        lblX.setImage(img);
426
 
                }
427
 
 
428
 
 
429
 
                filterDelayTimer = new Timer("Filter");
430
 
                filterDelayTimer.addEvent(SystemTime.getCurrentTime() + 300,
431
 
                                new TimerEventPerformer() {
432
 
                                        public void perform(TimerEvent event) {
433
 
                                                filterDelayTimer.destroy();
434
 
                                                filterDelayTimer = null;
435
 
 
436
 
                                                Utils.execSWTThread(new AERunnable() {
437
 
                                                        public void runSupport() {
438
 
                                                                // TODO Auto-generated method stub
439
 
                                                                ArrayList foundItems = new ArrayList();
440
 
                                                                TreeItem[] items = tree.getItems();
441
 
                                                                try {
442
 
                                                                        tree.setRedraw(false);
443
 
                                                                        for (int i = 0; i < items.length; i++) {
444
 
                                                                                items[i].setExpanded(false);
445
 
                                                                        }
446
 
 
447
 
                                                                        filterTree(items, filterText, foundItems);
448
 
                                                                } finally {
449
 
                                                                        tree.setRedraw(true);
450
 
                                                                }
451
 
                                                        }
452
 
                                                });
453
 
                                        }
454
 
                                });
455
 
        }
456
 
 
457
 
        protected void filterTree(TreeItem[] items, String text, ArrayList foundItems)
458
 
        {
459
 
                text = text.toLowerCase();
460
 
                for (int i = 0; i < items.length; i++) {
461
 
                        ensureSectionBuilt(items[i]);
462
 
                        ScrolledComposite composite = (ScrolledComposite) items[i].getData("Panel");
463
 
 
464
 
                        if (text.length() > 0
465
 
                                        && (items[i].getText().toLowerCase().indexOf(text) >= 0 || compositeHasText(
466
 
                                                        composite, text))) {
467
 
                                foundItems.add(items[i]);
468
 
 
469
 
                                ensureExpandedTo(items[i]);
470
 
                                items[i].setFont(filterFoundFont);
471
 
                        } else {
472
 
                                items[i].setFont(null);
473
 
                        }
474
 
                        filterTree(items[i].getItems(), text, foundItems);
475
 
                }
476
 
        }
477
 
        
478
 
        private void ensureExpandedTo(TreeItem item) {
479
 
    TreeItem itemParent = item.getParentItem();
480
 
        if (itemParent != null) {
481
 
                itemParent.setExpanded(true);
482
 
                ensureExpandedTo(itemParent);
483
 
        }
484
 
        }
485
 
 
486
 
        /**
487
 
         * @param composite 
488
 
         * @param text
489
 
         * @return
490
 
         */
491
 
        private boolean compositeHasText(Composite composite, String text) {
492
 
                Control[] children = composite.getChildren();
493
 
                
494
 
                for (int i = 0; i < children.length; i++) {
495
 
                        Control child = children[i];
496
 
                        if (child instanceof Label) {
497
 
                                if (((Label)child).getText().toLowerCase().indexOf(text) >= 0) {
498
 
                                        return true;
499
 
                                }
500
 
                        } else if (child instanceof Group) {
501
 
                                if (((Group)child).getText().toLowerCase().indexOf(text) >= 0) {
502
 
                                        return true;
503
 
                                }
504
 
                        } else if (child instanceof Button) {
505
 
                                if (((Button)child).getText().toLowerCase().indexOf(text) >= 0) {
506
 
                                        return true;
507
 
                                }
508
 
                        }
509
 
 
510
 
                        if (child instanceof Composite) {
511
 
                                if (compositeHasText((Composite) child, text)) {
512
 
                                        return true;
513
 
                                }
514
 
                        }
515
 
                }
516
 
                
517
 
                return false;
518
 
        }
519
 
 
520
 
        private void showSection(TreeItem section) {
521
 
    ScrolledComposite item = (ScrolledComposite)section.getData("Panel");
522
 
 
523
 
    if (item != null) {
524
 
        
525
 
        ensureSectionBuilt(section);
526
 
        
527
 
      layoutConfigSection.topControl = item;
528
 
      
529
 
      Composite c = (Composite)item.getContent();
530
 
      
531
 
      item.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
532
 
      cConfigSection.layout();
533
 
      
534
 
      updateHeader(section);
535
 
    }
536
 
  }
537
 
        
538
 
        private void ensureSectionBuilt(TreeItem section) {
539
 
    ScrolledComposite item = (ScrolledComposite)section.getData("Panel");
540
 
 
541
 
    if (item != null) {
542
 
        
543
 
      ConfigSection configSection = (ConfigSection)section.getData("ConfigSectionSWT");
544
 
      
545
 
      if (configSection != null) {
546
 
          
547
 
        Control previous = item.getContent();
548
 
        if (previous instanceof Composite) {
549
 
                configSection.configSectionDelete();
550
 
          Utils.disposeComposite((Composite)previous,true);
551
 
        }
552
 
        
553
 
        Composite c;
554
 
        
555
 
        if ( configSection instanceof ConfigSectionSWT ){
556
 
          
557
 
          c = ((ConfigSectionSWT)configSection).configSectionCreate(item);
558
 
          
559
 
        }else{
560
 
 
561
 
          c = ((UISWTConfigSection)configSection).configSectionCreate(item);
562
 
        }
563
 
        
564
 
        item.setContent(c);
565
 
      }
566
 
    }
567
 
        }
568
 
 
569
 
  private void updateHeader(TreeItem section) {
570
 
    if (section == null)
571
 
      return;
572
 
 
573
 
    String sHeader = section.getText();
574
 
    section = section.getParentItem();
575
 
    while (section != null) {
576
 
      sHeader = section.getText() + " : " + sHeader;
577
 
      section = section.getParentItem();
578
 
    }
579
 
    lHeader.setText(" " + sHeader.replaceAll("&", "&&"));
580
 
  }
581
 
 
582
 
 
583
 
  private Composite createConfigSection(String sNameID) {
584
 
    return createConfigSection(null, sNameID, -1, true);
585
 
  }
586
 
 
587
 
  private Composite createConfigSection(String sNameID, int position) {
588
 
    return createConfigSection(null, sNameID, position, true);
589
 
  }
590
 
 
591
 
  public Composite createConfigSection(TreeItem treeItemParent, 
592
 
                                        String sNameID, 
593
 
                                        int position, 
594
 
                                        boolean bPrefix) {
595
 
    ScrolledComposite sc = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
596
 
    sc.setExpandHorizontal(true);
597
 
    sc.setExpandVertical(true);
598
 
    sc.setLayoutData(new GridData(GridData.FILL_BOTH));
599
 
                sc.getVerticalBar().setIncrement(16);
600
 
                sc.addListener(SWT.Resize, new Listener() {
601
 
                        public void handleEvent(Event event) {
602
 
                                ScrolledComposite sc = (ScrolledComposite)event.widget;
603
 
                        sc.getVerticalBar().setPageIncrement(sc.getSize().y);
604
 
                        }
605
 
                });
606
 
 
607
 
    Composite cConfigSection = new Composite(sc, SWT.NULL);
608
 
    
609
 
    String section_key = ((bPrefix) ? sSectionPrefix : "") + sNameID;
610
 
    
611
 
    if (position == -2) { // Means "auto-order".
612
 
        position = findInsertPointFor(MessageText.getString(section_key), (treeItemParent == null) ? (Object)tree : (Object)treeItemParent);
613
 
    }
614
 
 
615
 
    TreeItem treeItem;
616
 
    if (treeItemParent == null) {
617
 
      if (position >= 0)
618
 
        treeItem = new TreeItem(tree, SWT.NULL, position);
619
 
      else
620
 
        treeItem = new TreeItem(tree, SWT.NULL);
621
 
    } else {
622
 
      if (position >= 0)
623
 
        treeItem = new TreeItem(treeItemParent, SWT.NULL, position);
624
 
      else
625
 
        treeItem = new TreeItem(treeItemParent, SWT.NULL);
626
 
    }
627
 
    Messages.setLanguageText(treeItem, section_key);
628
 
    treeItem.setData("Panel", sc);
629
 
    treeItem.setData("ID", sNameID);
630
 
 
631
 
    sc.setContent(cConfigSection);
632
 
    return cConfigSection;
633
 
  }
634
 
  
635
 
  private static Comparator insert_point_comparator = new Comparator() {
636
 
          
637
 
          private String asString(Object o) {
638
 
                  if (o instanceof String) {
639
 
                          return (String)o;
640
 
                  }
641
 
                  else if (o instanceof TreeItem) {
642
 
                          return ((TreeItem)o).getText();
643
 
                  }
644
 
                  else {
645
 
                          throw new ClassCastException("object is not String or TreeItem: " + o.getClass().getName());
646
 
                  }
647
 
          }
648
 
          
649
 
          public int compare(Object o1, Object o2) {
650
 
                  int result = String.CASE_INSENSITIVE_ORDER.compare(asString(o1), asString(o2));
651
 
                  return result;
652
 
          }
653
 
  };
654
 
  
655
 
  public static int findInsertPointFor(String name, Object structure) {
656
 
          TreeItem[] children = null;
657
 
          if (structure instanceof Tree) {
658
 
              children = ((Tree)structure).getItems();
659
 
          }
660
 
          else {
661
 
                  children = ((TreeItem)structure).getItems();
662
 
          }
663
 
          if (children.length == 0) {return -1;}
664
 
          int result =  Arrays.binarySearch(children, name, insert_point_comparator);
665
 
          if (result > 0) {return result;}
666
 
          result = -(result+1);
667
 
          if (result == children.length) {
668
 
                  result = -1;
669
 
          }
670
 
          return result;
671
 
  }
672
 
 
673
 
  public TreeItem findTreeItem(String ID) {
674
 
        return findTreeItem((Tree)null, ID);
675
 
  }
676
 
 
677
 
  private TreeItem findTreeItem(Tree tree, String ID) {
678
 
        if (tree == null)
679
 
                tree = this.tree;
680
 
    TreeItem[] items = tree.getItems();
681
 
    for (int i = 0; i < items.length; i++) {
682
 
      String itemID = (String)items[i].getData("ID");
683
 
      if (itemID != null && itemID.equalsIgnoreCase(ID)) {
684
 
        return items[i];
685
 
      }
686
 
      TreeItem itemFound = findTreeItem(items[i], ID);
687
 
      if (itemFound != null)
688
 
        return itemFound;
689
 
    }
690
 
         return null;
691
 
  }
692
 
 
693
 
  private TreeItem findTreeItem(TreeItem item, String ID) {
694
 
    TreeItem[] subItems = item.getItems();
695
 
    for (int i = 0; i < subItems.length; i++) {
696
 
      String itemID = (String)subItems[i].getData("ID");
697
 
      if (itemID != null && itemID.equalsIgnoreCase(ID)) {
698
 
        return subItems[i];
699
 
      }
700
 
 
701
 
      TreeItem itemFound = findTreeItem(subItems[i], ID);
702
 
      if (itemFound != null)
703
 
        return itemFound;
704
 
    }
705
 
    return null;
706
 
  }
707
 
 
708
 
  private void initSaveButton() {
709
 
    GridData gridData;
710
 
    final Button save = new Button(cConfig, SWT.PUSH);
711
 
    Messages.setLanguageText(save, "ConfigView.button.save"); //$NON-NLS-1$
712
 
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
713
 
    gridData.horizontalSpan = 2;
714
 
    gridData.widthHint = 80;
715
 
    save.setLayoutData(gridData);
716
 
 
717
 
    save.addSelectionListener(new SelectionAdapter() {
718
 
      public void widgetSelected(SelectionEvent event) {
719
 
                                // force focusout on osx
720
 
                                save.setFocus();
721
 
                                COConfigurationManager.setParameter("updated", 1);
722
 
                                COConfigurationManager.save();
723
 
 
724
 
                                for (int i = 0; i < pluginSections.size(); i++)
725
 
                                        ((ConfigSection) pluginSections.get(i)).configSectionSave();
726
 
                        }
727
 
                });
728
 
  }
729
 
 
730
 
  /* (non-Javadoc)
731
 
   * @see org.gudy.azureus2.ui.swt.IView#getComposite()
732
 
   */
733
 
  public Composite getComposite() {
734
 
    return cConfig;
735
 
  }
736
 
 
737
 
  public void updateLanguage() {
738
 
    super.updateLanguage();
739
 
    updateHeader(tree.getSelection()[0]);
740
 
//    cConfig.setSize(cConfig.computeSize(SWT.DEFAULT, SWT.DEFAULT));
741
 
  }
742
 
 
743
 
  public void delete() {
744
 
    for (int i = 0; i < pluginSections.size(); i++)
745
 
      ((ConfigSection)pluginSections.get(i)).configSectionDelete();
746
 
    pluginSections.clear();
747
 
    if(! tree.isDisposed()) {
748
 
            TreeItem[] items = tree.getItems();
749
 
            for (int i = 0; i < items.length; i++) {
750
 
              Composite c = (Composite)items[i].getData("Panel");
751
 
              Utils.disposeComposite(c);
752
 
              items[i].setData("Panel", null);
753
 
        
754
 
              items[i].setData("ConfigSectionSWT", null);
755
 
            }
756
 
    }
757
 
    Utils.disposeComposite(cConfig);
758
 
 
759
 
        Utils.disposeSWTObjects(new Object[] { headerFont, filterFoundFont });
760
 
                headerFont = null;
761
 
                filterFoundFont = null;
762
 
  }
763
 
 
764
 
  public String getFullTitle() {
765
 
    return MessageText.getString("ConfigView.title.full"); //$NON-NLS-1$
766
 
  }
767
 
 
768
 
  public boolean selectSection(String id) {
769
 
                TreeItem ti = findTreeItem(id);
770
 
                if (ti == null)
771
 
                        return false;
772
 
                tree.setSelection(new TreeItem[] { ti });
773
 
                showSection(ti);
774
 
                return true;
775
 
        }
776
 
 
777
 
  public void
778
 
  selectSection(
779
 
        Class   config_section_class )
780
 
  {
781
 
          TreeItem[]    items = tree.getItems();
782
 
          
783
 
          for (int i=0;i<items.length;i++){
784
 
                  
785
 
                  TreeItem      item = items[i];
786
 
                                
787
 
                  ConfigSection section = (ConfigSection)item.getData("ConfigSectionSWT");
788
 
                          
789
 
                  if ( section != null && section.getClass() == config_section_class ){
790
 
                                  
791
 
                          tree.setSelection( new TreeItem[]{ item });
792
 
                          
793
 
                          showSection( item );
794
 
                          
795
 
                          break;
796
 
                  }
797
 
          }
798
 
  }
799
 
}
 
1
/*
 
2
 * Created on 2 juil. 2003
 
3
 * Copyright (C) 2003, 2004, 2005, 2006 Aelitis, All Rights Reserved.
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 * This program is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
 * GNU General Public License for more details.
 
13
 * You should have received a copy of the GNU General Public License
 
14
 * along with this program; if not, write to the Free Software
 
15
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
16
 *
 
17
 * AELITIS, SAS au capital de 46,603.30 euros
 
18
 * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
 
19
 *
 
20
 */
 
21
package org.gudy.azureus2.ui.swt.views;
 
22
 
 
23
import java.util.ArrayList;
 
24
import java.util.Arrays;
 
25
import java.util.Comparator;
 
26
 
 
27
import org.eclipse.swt.SWT;
 
28
import org.eclipse.swt.custom.SashForm;
 
29
import org.eclipse.swt.custom.ScrolledComposite;
 
30
import org.eclipse.swt.custom.StackLayout;
 
31
import org.eclipse.swt.events.*;
 
32
import org.eclipse.swt.graphics.*;
 
33
import org.eclipse.swt.layout.*;
 
34
import org.eclipse.swt.widgets.*;
 
35
 
 
36
import org.gudy.azureus2.core3.config.COConfigurationManager;
 
37
import org.gudy.azureus2.core3.internat.MessageText;
 
38
import org.gudy.azureus2.core3.logging.LogEvent;
 
39
import org.gudy.azureus2.core3.logging.LogIDs;
 
40
import org.gudy.azureus2.core3.logging.Logger;
 
41
import org.gudy.azureus2.core3.util.*;
 
42
import org.gudy.azureus2.pluginsimpl.local.ui.config.ConfigSectionRepository;
 
43
import org.gudy.azureus2.ui.swt.ImageRepository;
 
44
import org.gudy.azureus2.ui.swt.Messages;
 
45
import org.gudy.azureus2.ui.swt.Utils;
 
46
import org.gudy.azureus2.ui.swt.plugins.UISWTConfigSection;
 
47
import org.gudy.azureus2.ui.swt.views.configsections.*;
 
48
 
 
49
import com.aelitis.azureus.core.AzureusCore;
 
50
 
 
51
import org.gudy.azureus2.plugins.ui.config.ConfigSection;
 
52
import org.gudy.azureus2.plugins.ui.config.ConfigSectionSWT;
 
53
 
 
54
/**
 
55
 * @author Olivier
 
56
 *
 
57
 */
 
58
public class ConfigView extends AbstractIView {
 
59
        private static final LogIDs LOGID = LogIDs.GUI;
 
60
  public static final String sSectionPrefix = "ConfigView.section.";
 
61
  
 
62
  AzureusCore           azureus_core;
 
63
  
 
64
  Composite cConfig;
 
65
  Composite cConfigSection;
 
66
  StackLayout layoutConfigSection;
 
67
  Label lHeader;
 
68
  Font headerFont;
 
69
  Font filterFoundFont;
 
70
  Tree tree;
 
71
  ArrayList pluginSections;
 
72
 
 
73
        private Timer filterDelayTimer;
 
74
        private String filterText = "";
 
75
        private Label lblX;
 
76
        private Listener scResizeListener;
 
77
 
 
78
  /**
 
79
   * Main Initializer
 
80
   * 
 
81
   * @param _azureus_core
 
82
   */
 
83
  public 
 
84
  ConfigView(
 
85
        AzureusCore             _azureus_core ) 
 
86
  {
 
87
        azureus_core    = _azureus_core;
 
88
  }
 
89
 
 
90
  /* (non-Javadoc)
 
91
   * @see org.gudy.azureus2.ui.swt.IView#initialize(org.eclipse.swt.widgets.Composite)
 
92
   */
 
93
  public void initialize(Composite composite) {
 
94
    GridData gridData;
 
95
    /*
 
96
    /--cConfig-----------------------------------------------------------\
 
97
    | ###SashForm#form################################################## |
 
98
    | # /--cLeftSide-\ /--cRightSide---------------------------------\ # |
 
99
    | # |txtFilter X | | ***cHeader********************************* | # |
 
100
    | # | ##tree#### | | * lHeader                                 * | # |
 
101
    | # | #        # | | ******************************************* | # |
 
102
    | # | #        # | | ###Composite cConfigSection################ | # |
 
103
    | # | #        # | | #                                         # | # |
 
104
    | # | #        # | | #                                         # | # |
 
105
    | # | #        # | | #                                         # | # |
 
106
    | # | #        # | | #                                         # | # |
 
107
    | # | ########## | | ########################################### | # |
 
108
    | # \------------/ \---------------------------------------------/ # |
 
109
    | ################################################################## |
 
110
    |  [Button]                                                          |
 
111
    \--------------------------------------------------------------------/
 
112
    */
 
113
    try {
 
114
      Display d = composite.getDisplay();
 
115
 
 
116
      cConfig = new Composite(composite, SWT.NONE);
 
117
      GridLayout configLayout = new GridLayout();
 
118
      configLayout.marginHeight = 0;
 
119
      configLayout.marginWidth = 0;
 
120
      cConfig.setLayout(configLayout);
 
121
      gridData = new GridData(GridData.FILL_BOTH);
 
122
      cConfig.setLayoutData(gridData);
 
123
  
 
124
      SashForm form = new SashForm(cConfig,SWT.HORIZONTAL);
 
125
      gridData = new GridData(GridData.FILL_BOTH);
 
126
      form.setLayoutData(gridData);
 
127
      
 
128
      Composite cLeftSide = new Composite(form, SWT.BORDER);
 
129
      gridData = new GridData(GridData.FILL_BOTH);
 
130
      cLeftSide.setLayoutData(gridData);
 
131
      
 
132
      FormLayout layout = new FormLayout();
 
133
      cLeftSide.setLayout(layout);
 
134
      
 
135
      Composite cFilterArea = new Composite(cLeftSide, SWT.NONE);
 
136
      cFilterArea.setLayout(new FormLayout());
 
137
      
 
138
      final Text txtFilter = new Text(cFilterArea, SWT.BORDER);
 
139
      final String sFilterText = MessageText.getString("ConfigView.filter");
 
140
      txtFilter.setText(sFilterText);
 
141
      txtFilter.selectAll();
 
142
      txtFilter.addModifyListener(new ModifyListener() {
 
143
        public void modifyText(ModifyEvent e) {
 
144
                filterTree(txtFilter.getText());
 
145
        }
 
146
      });
 
147
      txtFilter.addMouseListener(new MouseAdapter() {
 
148
                                public void mouseDown(MouseEvent e) {
 
149
                                        if (txtFilter.getText().equals(sFilterText)) {
 
150
                                                txtFilter.selectAll();
 
151
                                        }
 
152
                                }
 
153
                        });
 
154
      txtFilter.setFocus();
 
155
 
 
156
                lblX = new Label(cFilterArea, SWT.WRAP);
 
157
      Messages.setLanguageTooltip(lblX, "MyTorrentsView.clearFilter.tooltip");
 
158
      lblX.setImage(ImageRepository.getImage("smallx-gray"));
 
159
      lblX.addMouseListener(new MouseAdapter() {
 
160
        public void mouseUp(MouseEvent e) {
 
161
                txtFilter.setText("");
 
162
        }
 
163
      });
 
164
      
 
165
      Label lblSearch = new Label(cFilterArea, SWT.NONE);
 
166
      lblSearch.setImage(ImageRepository.getImage("search"));
 
167
  
 
168
      tree = new Tree(cLeftSide, SWT.NONE);
 
169
      FontData[] fontData = tree.getFont().getFontData();
 
170
      fontData[0].setStyle(SWT.BOLD);
 
171
      filterFoundFont = new Font(d, fontData);
 
172
      
 
173
      FormData formData;
 
174
 
 
175
      formData = new FormData();
 
176
      formData.bottom = new FormAttachment(100, -5);
 
177
      formData.left = new FormAttachment(0, 0);
 
178
      formData.right = new FormAttachment(100, 0);
 
179
      cFilterArea.setLayoutData(formData);
 
180
      
 
181
      formData = new FormData();
 
182
      formData.top = new FormAttachment(0,5);
 
183
      formData.left = new FormAttachment(0, 5);
 
184
      lblSearch.setLayoutData(formData);
 
185
 
 
186
      formData = new FormData();
 
187
      formData.top = new FormAttachment(0,5);
 
188
      formData.left = new FormAttachment(lblSearch,5);
 
189
      formData.right = new FormAttachment(lblX, -3);
 
190
      txtFilter.setLayoutData(formData);
 
191
 
 
192
      formData = new FormData();
 
193
      formData.top = new FormAttachment(0,5);
 
194
      formData.right = new FormAttachment(100,-5);
 
195
      lblX.setLayoutData(formData);
 
196
 
 
197
      formData = new FormData();
 
198
      formData.top = new FormAttachment(0, 0);
 
199
      formData.left = new FormAttachment(0,0);
 
200
      formData.right = new FormAttachment(100,0);
 
201
      formData.bottom = new FormAttachment(cFilterArea,-1);
 
202
      tree.setLayoutData(formData);
 
203
  
 
204
      Composite cRightSide = new Composite(form, SWT.NULL);
 
205
      configLayout = new GridLayout();
 
206
      configLayout.marginHeight = 3;
 
207
      configLayout.marginWidth = 0;
 
208
      cRightSide.setLayout(configLayout);
 
209
  
 
210
      // Header
 
211
      Composite cHeader = new Composite(cRightSide, SWT.BORDER);
 
212
      configLayout = new GridLayout();
 
213
      configLayout.marginHeight = 3;
 
214
      configLayout.marginWidth = 0;
 
215
      cHeader.setLayout(configLayout);
 
216
      gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
 
217
      cHeader.setLayoutData(gridData);
 
218
  
 
219
      cHeader.setBackground(d.getSystemColor(SWT.COLOR_LIST_SELECTION));
 
220
      cHeader.setForeground(d.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
 
221
  
 
222
      lHeader = new Label(cHeader, SWT.NULL);
 
223
      lHeader.setBackground(d.getSystemColor(SWT.COLOR_LIST_SELECTION));
 
224
      lHeader.setForeground(d.getSystemColor(SWT.COLOR_LIST_SELECTION_TEXT));
 
225
      fontData = lHeader.getFont().getFontData();
 
226
      fontData[0].setStyle(SWT.BOLD);
 
227
      int fontHeight = (int)(fontData[0].getHeight() * 1.2);
 
228
      fontData[0].setHeight(fontHeight);
 
229
      headerFont = new Font(d, fontData);
 
230
      lHeader.setFont(headerFont);
 
231
      gridData = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER);
 
232
      lHeader.setLayoutData(gridData);
 
233
  
 
234
      // Config Section
 
235
      cConfigSection = new Composite(cRightSide, SWT.NULL);
 
236
      layoutConfigSection = new StackLayout();
 
237
      cConfigSection.setLayout(layoutConfigSection);
 
238
      gridData = new GridData(GridData.FILL_BOTH);
 
239
      gridData.horizontalIndent = 2;
 
240
      cConfigSection.setLayoutData(gridData);
 
241
  
 
242
  
 
243
      form.setWeights(new int[] {20,80});
 
244
  
 
245
      tree.addSelectionListener(new SelectionAdapter() {
 
246
        public void widgetSelected(SelectionEvent e) {
 
247
          Tree tree = (Tree)e.getSource();
 
248
          //Check that at least an item is selected
 
249
          //OSX lets you select nothing in the tree for example when a child is selected
 
250
          //and you close its parent.
 
251
          if(tree.getSelection().length > 0)
 
252
              showSection(tree.getSelection()[0]);
 
253
           }
 
254
      });
 
255
      // Double click = expand/contract branch
 
256
      tree.addListener(SWT.DefaultSelection, new Listener() {
 
257
        public void handleEvent(Event e) {
 
258
            TreeItem item = (TreeItem)e.item;
 
259
            if (item != null)
 
260
              item.setExpanded(!item.getExpanded());
 
261
        }
 
262
      });
 
263
    } catch (Exception e) {
 
264
        Logger.log(new LogEvent(LOGID, "Error initializing ConfigView", e));
 
265
    }
 
266
 
 
267
    scResizeListener = new Listener() {
 
268
                        public void handleEvent(Event event) {
 
269
                                ScrolledComposite sc = (ScrolledComposite)event.widget;
 
270
 
 
271
              Composite c = (Composite)sc.getContent();
 
272
              if (c != null) {
 
273
                Point size1 = c.computeSize(sc.getClientArea().width,
 
274
                                                        SWT.DEFAULT);
 
275
                                        Point size = c.computeSize(SWT.DEFAULT, size1.y);
 
276
                sc.setMinSize(size);
 
277
              }
 
278
                        sc.getVerticalBar().setPageIncrement(sc.getSize().y);
 
279
                        }
 
280
                };
 
281
 
 
282
 
 
283
    // Add sections
 
284
    /** How to add a new section
 
285
     * 1) Create a new implementation of ConfigSectionSWT in a new file
 
286
     *    (Use the ConfigSectionTMP.java as a template if it's still around)
 
287
     * 2) import it into here
 
288
     * 3) add it to the internal sections list
 
289
     */
 
290
    pluginSections = ConfigSectionRepository.getInstance().getList();
 
291
 
 
292
    ConfigSection[] internalSections = { new ConfigSectionConnection(),
 
293
                                         new ConfigSectionConnectionProxy(),
 
294
                                         new ConfigSectionConnectionAdvanced(),
 
295
                                         new ConfigSectionConnectionEncryption(),
 
296
                                         new ConfigSectionTransfer(azureus_core),
 
297
                                         new ConfigSectionTransferAutoSpeedSelect(),
 
298
                                         new ConfigSectionTransferAutoSpeed(),
 
299
                                         new ConfigSectionTransferAutoSpeedBeta(),
 
300
                                         new ConfigSectionTransferLAN(),
 
301
                                         new ConfigSectionFile(), 
 
302
                                         new ConfigSectionFileMove(),
 
303
                                         new ConfigSectionFileTorrents(),
 
304
                                         new ConfigSectionFileTorrentsDecoding(),
 
305
                                         new ConfigSectionFilePerformance(),
 
306
                                         new ConfigSectionInterface(),
 
307
                                         new ConfigSectionInterfaceLanguage(),
 
308
                                         new ConfigSectionInterfaceStart(),
 
309
                                         new ConfigSectionInterfaceDisplay(),
 
310
                                         new ConfigSectionInterfaceColor(),
 
311
                                         new ConfigSectionInterfaceAlerts(),
 
312
                                         new ConfigSectionMode(),
 
313
                                         new ConfigSectionIPFilter(azureus_core),
 
314
                                         new ConfigSectionPlugins(this, azureus_core),
 
315
                                         new ConfigSectionStats(),
 
316
                                         new ConfigSectionTracker(azureus_core),
 
317
                                         new ConfigSectionTrackerClient(),
 
318
                                         new ConfigSectionTrackerServer(azureus_core),
 
319
                                         new ConfigSectionSecurity(),
 
320
                                         new ConfigSectionSharing(),
 
321
                                         new ConfigSectionLogging()
 
322
                                        };
 
323
    
 
324
    pluginSections.addAll(0, Arrays.asList(internalSections));
 
325
 
 
326
    for (int i = 0; i < pluginSections.size(); i++) {
 
327
   
 
328
        // slip the non-standard "plugins" initialisation inbetween the internal ones
 
329
        // and the plugin ones so plugin ones can be children of it
 
330
        
 
331
      boolean   plugin_section = i >= internalSections.length;
 
332
      
 
333
      ConfigSection section = (ConfigSection)pluginSections.get(i);
 
334
      
 
335
      if (section instanceof ConfigSectionSWT || section instanceof UISWTConfigSection ) {
 
336
        String name;
 
337
        try {
 
338
          name = section.configSectionGetName();
 
339
         } catch (Exception e) {
 
340
                 Logger.log(new LogEvent(LOGID, "A ConfigSection plugin caused an "
 
341
                                                        + "error while trying to call its "
 
342
                                                        + "configSectionGetName function", e));
 
343
          name = "Bad Plugin";
 
344
        }
 
345
 
 
346
         String section_key = name;
 
347
         
 
348
         if ( plugin_section ){
 
349
                        // if resource exists without prefix then use it as plugins don't
 
350
                        // need to start with the prefix
 
351
                
 
352
                if ( !MessageText.keyExists(section_key)){
 
353
                        
 
354
                        section_key = sSectionPrefix + name;
 
355
                }
 
356
                
 
357
         }else{
 
358
                
 
359
                section_key = sSectionPrefix + name;
 
360
         }
 
361
         
 
362
         try {
 
363
          TreeItem treeItem;
 
364
          String location = section.configSectionGetParentSection();
 
365
  
 
366
          if (location.equalsIgnoreCase(ConfigSection.SECTION_ROOT))
 
367
                  treeItem = new TreeItem(tree, SWT.NULL);
 
368
          else if (location != "") {
 
369
                  TreeItem treeItemFound = findTreeItem(tree, location);
 
370
                  if (treeItemFound != null){
 
371
                          if (location.equalsIgnoreCase(ConfigSection.SECTION_PLUGINS)) {
 
372
                                  // Force ordering by name here.
 
373
                                  int position = findInsertPointFor(MessageText.getString(section_key), treeItemFound);
 
374
                                  if (position == -1) {
 
375
                                          treeItem = new TreeItem(treeItemFound, SWT.NULL);
 
376
                                  }
 
377
                                  else {
 
378
                                          treeItem = new TreeItem(treeItemFound, SWT.NULL, position);
 
379
                                  }
 
380
                          }
 
381
                          else {
 
382
                                  treeItem = new TreeItem(treeItemFound, SWT.NULL);
 
383
                          }
 
384
                  }else{
 
385
                          treeItem = new TreeItem(tree, SWT.NULL);
 
386
                  }
 
387
          }else{
 
388
                  treeItem = new TreeItem(tree, SWT.NULL); 
 
389
          }
 
390
  
 
391
          ScrolledComposite sc = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
 
392
          sc.setExpandHorizontal(true);
 
393
          sc.setExpandVertical(true);
 
394
          sc.setLayoutData(new GridData(GridData.FILL_BOTH));
 
395
                sc.getVerticalBar().setIncrement(16);
 
396
                sc.addListener(SWT.Resize, scResizeListener);
 
397
          
 
398
          if(i == 0) {
 
399
            Composite c;
 
400
            if ( section instanceof ConfigSectionSWT ){
 
401
                  
 
402
                  c = ((ConfigSectionSWT)section).configSectionCreate(sc);
 
403
                  
 
404
            }else{
 
405
   
 
406
                  c = ((UISWTConfigSection)section).configSectionCreate(sc);
 
407
            }
 
408
            sc.setContent(c);
 
409
          }
 
410
          
 
411
          Messages.setLanguageText(treeItem, section_key);
 
412
          treeItem.setData("Panel", sc);
 
413
          treeItem.setData("ID", name);
 
414
          treeItem.setData("ConfigSectionSWT", section);
 
415
          
 
416
          // ConfigSectionPlugins is special because it has to handle the
 
417
          // PluginConfigModel config pages
 
418
          if (section instanceof ConfigSectionPlugins)
 
419
                ((ConfigSectionPlugins)section).initPluginSubSections();
 
420
        } catch (Exception e) {
 
421
                Logger.log(new LogEvent(LOGID, "ConfigSection plugin '" + name
 
422
                                                        + "' caused an error", e));
 
423
        }
 
424
      }
 
425
    }
 
426
    
 
427
 
 
428
 
 
429
 
 
430
    initSaveButton();
 
431
 
 
432
    TreeItem[] items = { tree.getItems()[0] };
 
433
    tree.setSelection(items);
 
434
    // setSelection doesn't trigger a SelectionListener, so..
 
435
    showSection(items[0]);
 
436
  }
 
437
 
 
438
  /**
 
439
         * @param text
 
440
         */
 
441
        protected void filterTree(String text) {
 
442
                filterText = text;
 
443
                if (filterDelayTimer != null) {
 
444
                        filterDelayTimer.destroy();
 
445
                }
 
446
                
 
447
                if (lblX != null && !lblX.isDisposed()) {
 
448
                        Image img = ImageRepository.getImage(filterText.length() > 0 ? "smallx"
 
449
                                        : "smallx-gray");
 
450
 
 
451
                        lblX.setImage(img);
 
452
                }
 
453
 
 
454
 
 
455
                filterDelayTimer = new Timer("Filter");
 
456
                filterDelayTimer.addEvent(SystemTime.getCurrentTime() + 300,
 
457
                                new TimerEventPerformer() {
 
458
                                        public void perform(TimerEvent event) {
 
459
                                                filterDelayTimer.destroy();
 
460
                                                filterDelayTimer = null;
 
461
 
 
462
                                                Utils.execSWTThread(new AERunnable() {
 
463
                                                        public void runSupport() {
 
464
                                                                // TODO Auto-generated method stub
 
465
                                                                ArrayList foundItems = new ArrayList();
 
466
                                                                TreeItem[] items = tree.getItems();
 
467
                                                                try {
 
468
                                                                        tree.setRedraw(false);
 
469
                                                                        for (int i = 0; i < items.length; i++) {
 
470
                                                                                items[i].setExpanded(false);
 
471
                                                                        }
 
472
 
 
473
                                                                        filterTree(items, filterText, foundItems);
 
474
                                                                } finally {
 
475
                                                                        tree.setRedraw(true);
 
476
                                                                }
 
477
                                                        }
 
478
                                                });
 
479
                                        }
 
480
                                });
 
481
        }
 
482
 
 
483
        protected void filterTree(TreeItem[] items, String text, ArrayList foundItems)
 
484
        {
 
485
                text = text.toLowerCase();
 
486
                for (int i = 0; i < items.length; i++) {
 
487
                        ensureSectionBuilt(items[i]);
 
488
                        ScrolledComposite composite = (ScrolledComposite) items[i].getData("Panel");
 
489
 
 
490
                        if (text.length() > 0
 
491
                                        && (items[i].getText().toLowerCase().indexOf(text) >= 0 || compositeHasText(
 
492
                                                        composite, text))) {
 
493
                                foundItems.add(items[i]);
 
494
 
 
495
                                ensureExpandedTo(items[i]);
 
496
                                items[i].setFont(filterFoundFont);
 
497
                        } else {
 
498
                                items[i].setFont(null);
 
499
                        }
 
500
                        filterTree(items[i].getItems(), text, foundItems);
 
501
                }
 
502
        }
 
503
        
 
504
        private void ensureExpandedTo(TreeItem item) {
 
505
    TreeItem itemParent = item.getParentItem();
 
506
        if (itemParent != null) {
 
507
                itemParent.setExpanded(true);
 
508
                ensureExpandedTo(itemParent);
 
509
        }
 
510
        }
 
511
 
 
512
        /**
 
513
         * @param composite 
 
514
         * @param text
 
515
         * @return
 
516
         */
 
517
        private boolean compositeHasText(Composite composite, String text) {
 
518
                Control[] children = composite.getChildren();
 
519
                
 
520
                for (int i = 0; i < children.length; i++) {
 
521
                        Control child = children[i];
 
522
                        if (child instanceof Label) {
 
523
                                if (((Label)child).getText().toLowerCase().indexOf(text) >= 0) {
 
524
                                        return true;
 
525
                                }
 
526
                        } else if (child instanceof Group) {
 
527
                                if (((Group)child).getText().toLowerCase().indexOf(text) >= 0) {
 
528
                                        return true;
 
529
                                }
 
530
                        } else if (child instanceof Button) {
 
531
                                if (((Button)child).getText().toLowerCase().indexOf(text) >= 0) {
 
532
                                        return true;
 
533
                                }
 
534
                        }
 
535
 
 
536
                        if (child instanceof Composite) {
 
537
                                if (compositeHasText((Composite) child, text)) {
 
538
                                        return true;
 
539
                                }
 
540
                        }
 
541
                }
 
542
                
 
543
                return false;
 
544
        }
 
545
 
 
546
        private void showSection(TreeItem section) {
 
547
    ScrolledComposite item = (ScrolledComposite)section.getData("Panel");
 
548
 
 
549
    if (item != null) {
 
550
        
 
551
        ensureSectionBuilt(section);
 
552
        
 
553
      layoutConfigSection.topControl = item;
 
554
      
 
555
//      Composite c = (Composite)item.getContent();
 
556
      
 
557
 
 
558
      // resize listener will sc.setMinSize
 
559
//      System.out.println("show");
 
560
//      item.setMinSize(c.computeSize(SWT.DEFAULT, SWT.DEFAULT));
 
561
      cConfigSection.layout();
 
562
      
 
563
      updateHeader(section);
 
564
    }
 
565
  }
 
566
        
 
567
        private void ensureSectionBuilt(TreeItem section) {
 
568
    ScrolledComposite item = (ScrolledComposite)section.getData("Panel");
 
569
 
 
570
    if (item != null) {
 
571
        
 
572
      ConfigSection configSection = (ConfigSection)section.getData("ConfigSectionSWT");
 
573
      
 
574
      if (configSection != null) {
 
575
          
 
576
        Control previous = item.getContent();
 
577
        if (previous instanceof Composite) {
 
578
                configSection.configSectionDelete();
 
579
          Utils.disposeComposite((Composite)previous,true);
 
580
        }
 
581
        
 
582
        Composite c;
 
583
        
 
584
        if ( configSection instanceof ConfigSectionSWT ){
 
585
          
 
586
          c = ((ConfigSectionSWT)configSection).configSectionCreate(item);
 
587
          
 
588
        }else{
 
589
 
 
590
          c = ((UISWTConfigSection)configSection).configSectionCreate(item);
 
591
        }
 
592
        
 
593
        item.setContent(c);
 
594
      }
 
595
    }
 
596
        }
 
597
 
 
598
  private void updateHeader(TreeItem section) {
 
599
    if (section == null)
 
600
      return;
 
601
 
 
602
    String sHeader = section.getText();
 
603
    section = section.getParentItem();
 
604
    while (section != null) {
 
605
      sHeader = section.getText() + " : " + sHeader;
 
606
      section = section.getParentItem();
 
607
    }
 
608
    lHeader.setText(" " + sHeader.replaceAll("&", "&&"));
 
609
  }
 
610
 
 
611
 
 
612
  private Composite createConfigSection(String sNameID) {
 
613
    return createConfigSection(null, sNameID, -1, true);
 
614
  }
 
615
 
 
616
  private Composite createConfigSection(String sNameID, int position) {
 
617
    return createConfigSection(null, sNameID, position, true);
 
618
  }
 
619
 
 
620
  public Composite createConfigSection(TreeItem treeItemParent, 
 
621
                                        String sNameID, 
 
622
                                        int position, 
 
623
                                        boolean bPrefix) {
 
624
    ScrolledComposite sc = new ScrolledComposite(cConfigSection, SWT.H_SCROLL | SWT.V_SCROLL);
 
625
    sc.setExpandHorizontal(true);
 
626
    sc.setExpandVertical(true);
 
627
    sc.setLayoutData(new GridData(GridData.FILL_BOTH));
 
628
                sc.getVerticalBar().setIncrement(16);
 
629
                sc.addListener(SWT.Resize, scResizeListener);
 
630
 
 
631
    Composite cConfigSection = new Composite(sc, SWT.NULL);
 
632
    
 
633
    String section_key = ((bPrefix) ? sSectionPrefix : "") + sNameID;
 
634
    
 
635
    if (position == -2) { // Means "auto-order".
 
636
        position = findInsertPointFor(MessageText.getString(section_key), (treeItemParent == null) ? (Object)tree : (Object)treeItemParent);
 
637
    }
 
638
 
 
639
    TreeItem treeItem;
 
640
    if (treeItemParent == null) {
 
641
      if (position >= 0)
 
642
        treeItem = new TreeItem(tree, SWT.NULL, position);
 
643
      else
 
644
        treeItem = new TreeItem(tree, SWT.NULL);
 
645
    } else {
 
646
      if (position >= 0)
 
647
        treeItem = new TreeItem(treeItemParent, SWT.NULL, position);
 
648
      else
 
649
        treeItem = new TreeItem(treeItemParent, SWT.NULL);
 
650
    }
 
651
    Messages.setLanguageText(treeItem, section_key);
 
652
    treeItem.setData("Panel", sc);
 
653
    treeItem.setData("ID", sNameID);
 
654
 
 
655
    sc.setContent(cConfigSection);
 
656
    return cConfigSection;
 
657
  }
 
658
  
 
659
  private static Comparator insert_point_comparator = new Comparator() {
 
660
          
 
661
          private String asString(Object o) {
 
662
                  if (o instanceof String) {
 
663
                          return (String)o;
 
664
                  }
 
665
                  else if (o instanceof TreeItem) {
 
666
                          return ((TreeItem)o).getText();
 
667
                  }
 
668
                  else {
 
669
                          throw new ClassCastException("object is not String or TreeItem: " + o.getClass().getName());
 
670
                  }
 
671
          }
 
672
          
 
673
          public int compare(Object o1, Object o2) {
 
674
                  int result = String.CASE_INSENSITIVE_ORDER.compare(asString(o1), asString(o2));
 
675
                  return result;
 
676
          }
 
677
  };
 
678
  
 
679
  public static int findInsertPointFor(String name, Object structure) {
 
680
          TreeItem[] children = null;
 
681
          if (structure instanceof Tree) {
 
682
              children = ((Tree)structure).getItems();
 
683
          }
 
684
          else {
 
685
                  children = ((TreeItem)structure).getItems();
 
686
          }
 
687
          if (children.length == 0) {return -1;}
 
688
          int result =  Arrays.binarySearch(children, name, insert_point_comparator);
 
689
          if (result > 0) {return result;}
 
690
          result = -(result+1);
 
691
          if (result == children.length) {
 
692
                  result = -1;
 
693
          }
 
694
          return result;
 
695
  }
 
696
 
 
697
  public TreeItem findTreeItem(String ID) {
 
698
        return findTreeItem((Tree)null, ID);
 
699
  }
 
700
 
 
701
  private TreeItem findTreeItem(Tree tree, String ID) {
 
702
        if (tree == null)
 
703
                tree = this.tree;
 
704
    TreeItem[] items = tree.getItems();
 
705
    for (int i = 0; i < items.length; i++) {
 
706
      String itemID = (String)items[i].getData("ID");
 
707
      if (itemID != null && itemID.equalsIgnoreCase(ID)) {
 
708
        return items[i];
 
709
      }
 
710
      TreeItem itemFound = findTreeItem(items[i], ID);
 
711
      if (itemFound != null)
 
712
        return itemFound;
 
713
    }
 
714
         return null;
 
715
  }
 
716
 
 
717
  private TreeItem findTreeItem(TreeItem item, String ID) {
 
718
    TreeItem[] subItems = item.getItems();
 
719
    for (int i = 0; i < subItems.length; i++) {
 
720
      String itemID = (String)subItems[i].getData("ID");
 
721
      if (itemID != null && itemID.equalsIgnoreCase(ID)) {
 
722
        return subItems[i];
 
723
      }
 
724
 
 
725
      TreeItem itemFound = findTreeItem(subItems[i], ID);
 
726
      if (itemFound != null)
 
727
        return itemFound;
 
728
    }
 
729
    return null;
 
730
  }
 
731
 
 
732
  private void initSaveButton() {
 
733
    GridData gridData;
 
734
    final Button save = new Button(cConfig, SWT.PUSH);
 
735
    Messages.setLanguageText(save, "ConfigView.button.save"); //$NON-NLS-1$
 
736
    gridData = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
 
737
    gridData.horizontalSpan = 2;
 
738
    gridData.widthHint = 80;
 
739
    save.setLayoutData(gridData);
 
740
 
 
741
    save.addSelectionListener(new SelectionAdapter() {
 
742
      public void widgetSelected(SelectionEvent event) {
 
743
                                // force focusout on osx
 
744
                                save.setFocus();
 
745
                                COConfigurationManager.setParameter("updated", 1);
 
746
                                COConfigurationManager.save();
 
747
 
 
748
                                for (int i = 0; i < pluginSections.size(); i++)
 
749
                                        ((ConfigSection) pluginSections.get(i)).configSectionSave();
 
750
                        }
 
751
                });
 
752
  }
 
753
 
 
754
  /* (non-Javadoc)
 
755
   * @see org.gudy.azureus2.ui.swt.IView#getComposite()
 
756
   */
 
757
  public Composite getComposite() {
 
758
    return cConfig;
 
759
  }
 
760
 
 
761
  public void updateLanguage() {
 
762
    super.updateLanguage();
 
763
    updateHeader(tree.getSelection()[0]);
 
764
//    cConfig.setSize(cConfig.computeSize(SWT.DEFAULT, SWT.DEFAULT));
 
765
  }
 
766
 
 
767
  public void delete() {
 
768
    for (int i = 0; i < pluginSections.size(); i++) {
 
769
        try {
 
770
                ((ConfigSection)pluginSections.get(i)).configSectionDelete();
 
771
        } catch (Exception e) {
 
772
                Debug.out("Error while deleting config section", e);
 
773
        }
 
774
    }
 
775
    pluginSections.clear();
 
776
    if(! tree.isDisposed()) {
 
777
            TreeItem[] items = tree.getItems();
 
778
            for (int i = 0; i < items.length; i++) {
 
779
              Composite c = (Composite)items[i].getData("Panel");
 
780
              Utils.disposeComposite(c);
 
781
              items[i].setData("Panel", null);
 
782
        
 
783
              items[i].setData("ConfigSectionSWT", null);
 
784
            }
 
785
    }
 
786
    Utils.disposeComposite(cConfig);
 
787
 
 
788
        Utils.disposeSWTObjects(new Object[] { headerFont, filterFoundFont });
 
789
                headerFont = null;
 
790
                filterFoundFont = null;
 
791
  }
 
792
 
 
793
  public String getFullTitle() {
 
794
    return MessageText.getString("ConfigView.title.full"); //$NON-NLS-1$
 
795
  }
 
796
 
 
797
  public boolean selectSection(String id) {
 
798
                TreeItem ti = findTreeItem(id);
 
799
                if (ti == null)
 
800
                        return false;
 
801
                tree.setSelection(new TreeItem[] { ti });
 
802
                showSection(ti);
 
803
                return true;
 
804
        }
 
805
 
 
806
  public void
 
807
  selectSection(
 
808
        Class   config_section_class )
 
809
  {
 
810
          TreeItem[]    items = tree.getItems();
 
811
          
 
812
          for (int i=0;i<items.length;i++){
 
813
                  
 
814
                  TreeItem      item = items[i];
 
815
                                
 
816
                  ConfigSection section = (ConfigSection)item.getData("ConfigSectionSWT");
 
817
                          
 
818
                  if ( section != null && section.getClass() == config_section_class ){
 
819
                                  
 
820
                          tree.setSelection( new TreeItem[]{ item });
 
821
                          
 
822
                          showSection( item );
 
823
                          
 
824
                          break;
 
825
                  }
 
826
          }
 
827
  }
 
828
}