Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c1c23a2f6c72a44d83b0f9f9f844ac34e4eb3845 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**********************************************************************
 * Copyright (c) 2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: 
 * IBM - Initial API and implementation
 **********************************************************************/
package org.eclipse.cdt.ui.dialogs;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.ICDescriptor;
import org.eclipse.cdt.internal.core.search.indexing.IndexManager;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.index.AbstractIndexerPage;
import org.eclipse.cdt.utils.ui.controls.ControlFactory;
import org.eclipse.cdt.utils.ui.controls.TabFolderLayout;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.QualifiedName;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author Bogdan Gheorghe
 */

/**
 * This <code>IndexerBlock2</code> is used in the <code>MakeProjectWizardOptionPage</code> and
 * the <code>NewManagedProjectOptionPage</code> to display the indexer options during the creation of
 * a new project.
 */

public class IndexerBlock extends AbstractCOptionPage {

	public final static String INDEX_UI_ID = CUIPlugin.PLUGIN_ID + ".cdtindexers"; //$NON-NLS-1$
	public final static String INDEXERUIID = "indexerUIID"; //$NON-NLS-1$
	public final static QualifiedName indexerUIIDKey = new QualifiedName(INDEX_UI_ID, INDEXERUIID);
	
	public static final String INDEXER_UI = "indexerUI"; //$NON-NLS-1$
	public static final String INDEXER_UI_VALUE = "indexerUIValue"; //$NON-NLS-1$
	
	private static final String INDEXER_LABEL = CUIPlugin.getResourceString("BaseIndexerBlock.label" ); //$NON-NLS-1$
	private static final String INDEXER_DESCRIPTION = CUIPlugin.getResourceString("BaseIndexerBlock.desc"); //$NON-NLS-1$
	private static final String INDEXER_COMBO_LABEL = CUIPlugin.getResourceString("BaseIndexerBlock.comboLabel"); //$NON-NLS-1$
	
	
    private Combo 					indexersComboBox;
    private HashMap 				indexerPageMap;
    private String 					selectedIndexerId = null;
	private Composite 				parentComposite;
    private ICOptionPage 		 	currentPage;
    
    
    public IndexerBlock(){
		super(INDEXER_LABEL);
		setDescription(INDEXER_DESCRIPTION);
		initializeIndexerPageMap();
	}
    
    /**
     * Create a profile page only on request
     */
    protected static class IndexerPageConfiguration {

        ICOptionPage page;
        IConfigurationElement element;

        public IndexerPageConfiguration(IConfigurationElement _element) {
            element = _element;
        }

        public ICOptionPage getPage() throws CoreException {
            if (page == null) {
                page = (ICOptionPage) element.createExecutableExtension("class"); //$NON-NLS-1$
            }
            return page;
        }
        public String getName() {
            return element.getAttribute("name"); //$NON-NLS-1$
        }
        public String getIndexerID(){
        	return element.getAttribute("indexerID"); //$NON-NLS-1$
        }
    }

    
    /* (non-Javadoc)
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite parent) {
       
        
        Composite composite = ControlFactory.createComposite(parent, 1);
        Font font = parent.getFont();
		GridLayout layout=  ((GridLayout)composite.getLayout());
		layout.marginHeight= 0;
		layout.marginWidth= 0;
		layout.verticalSpacing= 0;
		layout.horizontalSpacing= GridData.FILL_HORIZONTAL;
		composite.setFont(font);
		setControl(composite);
      
		  
	    // create a composite for general scanner config discovery options
        Composite scComp = ControlFactory.createComposite(composite, 1);
        ((GridLayout)scComp.getLayout()).marginHeight = 0;
        ((GridLayout)scComp.getLayout()).marginTop = 5;
        scComp.setFont(font);
	        
        // Create a group for discovered indexer's UI
        if (createIndexerControls(scComp)) {
          // create a composite for discovery profile options
            Composite indexPageComposite = ControlFactory.createComposite(composite, 1);
            indexPageComposite.setFont(font);
            GridData gd = (GridData) indexPageComposite.getLayoutData();
            gd.grabExcessHorizontalSpace = true;
    //        gd.grabExcessVerticalSpace = true;
            gd.horizontalAlignment = GridData.FILL;
            gd.verticalAlignment = GridData.FILL;
            indexPageComposite.setLayout(new TabFolderLayout());
            // Must set the composite parent to super class.
            parentComposite = indexPageComposite;
            
            setPage();
           
        }
        parent.layout(true);
    }

 

	/**
	 * 
	 */
	private void setPage() {

        String profileId = getCurrentIndexPageId();
        
        //If no indexer has been selected, return
        if (profileId == null)
        	return;
        
        ICOptionPage page = getIndexerPage(profileId);
        if (page != null) {
            if (page.getControl() == null) {
                page.setContainer(getContainer());
                page.createControl(parentComposite);
                parentComposite.layout(true);
                parentComposite.pack(true);
            } else {
                page.setVisible(false);
            }
            page.setVisible(true);
        }
		setCurrentPage(page);
	}


	/**
	 * @param page
	 */
	private void setCurrentPage(ICOptionPage page) {
		currentPage = page;
	}



	protected String getCurrentIndexPageId() {
        String selectedIndexPageName = getSelectedIndexerID();
        
        if (selectedIndexPageName == null)
        	return null;
        
        String selectedIndexPageId = getIndexerPageId(selectedIndexPageName);
        return selectedIndexPageId;
    }
	/**
     * @param scComp
     * @param numberOfColumns
     * @return
     */
    private boolean createIndexerControls(Composite parent) {
        //TODO: Put in some logic to deal with old CDT project: upgrade old projects
        //to use the Classic CDT Indexer
        
        Group group= ControlFactory.createGroup(parent,INDEXER_COMBO_LABEL,2);
       
       // Add discovered indexers combo box
        indexersComboBox = ControlFactory.createSelectCombo(group,"", ""); //$NON-NLS-1$ //$NON-NLS-2$
  	
        //Add combo box listener
        indexersComboBox.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setPage();
			}
		});
        //Add button
        ControlFactory.createPushButton(group,"Info ...");
        
        // fill the combobox and set the initial value
        for (Iterator items = getIndexerPageIdList().iterator(); items.hasNext();) {
            String profileId = (String)items.next();
            String pageName = getIndexerPageName(profileId);
            if (pageName != null) {
                indexersComboBox.add(pageName);
            }
        }

        return true;
    }
    
    
    
    
    /**
     * Adds all the contributed Indexer Pages to a map
     */
    private void initializeIndexerPageMap() {
        indexerPageMap = new HashMap(5);
        
        IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(CUIPlugin.getPluginId(), "IndexerPage"); //$NON-NLS-1$
        IConfigurationElement[] infos = extensionPoint.getConfigurationElements();
        for (int i = 0; i < infos.length; i++) {
            if (infos[i].getName().equals("indexerUI")) { //$NON-NLS-1$
                String id = infos[i].getAttribute("id"); //$NON-NLS-1$
                indexerPageMap.put(id, new IndexerPageConfiguration(infos[i]));
            }
        }
    }

    
    /**
     * Returns the contributed Indexer Pages as a list
     */
    protected List getIndexerPageIdList() {
        return new ArrayList(indexerPageMap.keySet());
    }
  
    /**
     * Returns the page name for the given id
     * @param profileId 
     * @return String 
     */
    protected String getIndexerPageName(String indexerPageId) {
        IndexerPageConfiguration configElement = 
                (IndexerPageConfiguration) indexerPageMap.get(indexerPageId);
        if (configElement != null) {
            return configElement.getName();
        }
        return null;
    }
    /**
     * Returns the indexer page id for the give name
     * @param indexerPageName 
     * @return String 
     */
    protected String getIndexerPageId(String indexerPageName) {
        for (Iterator I = indexerPageMap.keySet().iterator(); I.hasNext();) {
            String indexerPageId = (String) I.next();
            String tempPageName = getIndexerPageName(indexerPageId);
            if (indexerPageName.equals(tempPageName)) {
                return indexerPageId;
            }
        }
        return null;
    }
    
    protected ICOptionPage getIndexerPage(String indexerPageId) {
        IndexerPageConfiguration configElement = 
                (IndexerPageConfiguration) indexerPageMap.get(indexerPageId);
        if (configElement != null) {
            try {
                return configElement.getPage();
            } catch (CoreException e) {
            }
        }
        return null;
    }
    
    /**
     * Returns the indexer id for the given name
     * @param profileId 
     * @return String 
     */
    protected String getIndexerIdName(String indexerPageId) {
        IndexerPageConfiguration configElement = 
                (IndexerPageConfiguration) indexerPageMap.get(indexerPageId);
        if (configElement != null) {
            return configElement.getIndexerID();
        }
        return null;
    }
    
    public void performApply(IProgressMonitor monitor) throws CoreException {
    	IProject newProject = null;
		newProject = getContainer().getProject();
	
		persistIndexerSettings(newProject, monitor);
    }

    /**
     * Persists BasicIndexerBlock settings to disk and allows current indexer page to persist settings
     * This is needed since we need to pass in the project if we are trying to save changes made to the 
     * property page.
     */
    public void persistIndexerSettings(IProject project, IProgressMonitor monitor) throws CoreException{
    	
    	persistIndexerValues(project);
		
		//Give the chosen indexer a chance to persist its values
		if (currentPage != null){
			currentPage.performApply(monitor);
		}
    }
    
    public void performDefaults() {
        // TODO Auto-generated method stub
        
    }

	/**
	 * @return
	 */
	public boolean isIndexEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	//Storage section
	
	public void persistIndexerValues(IProject project){
		ICDescriptor descriptor = null;
		Element rootElement = null;
		IProject newProject = null;
		
		//Get the currently selected indexer from the UI
		String indexerName = getSelectedIndexerID();
		//If no indexer has been selected, return
		if (indexerName == null)
			return; 
		
		//Match up the selected indexer in the UI to a corresponding 
		//contributed Indexer Page ID
		String indexerPageID = getIndexerPageId(indexerName);
		
		if (indexerPageID == null)
			return;
	
		//Get the id of the indexer that goes along with this UI page - this gets persisted
		String indexerID = getIndexerIdName(indexerPageID);
		
		try {
			newProject = project;
			descriptor = CCorePlugin.getDefault().getCProjectDescription(newProject, true);
			rootElement = descriptor.getProjectData(IndexManager.CDT_INDEXER);
		
			// Clear out all current children
			Node child = rootElement.getFirstChild();
			while (child != null) {
				rootElement.removeChild(child);
				child = rootElement.getFirstChild();
			}
			Document doc = rootElement.getOwnerDocument();
			
			saveIndexerInfo(indexerID, indexerPageID, rootElement, doc);
		
			descriptor.saveProjectData();
			
			//Update project session property
			
			project.setSessionProperty(IndexManager.indexerIDKey, indexerID);	
			project.setSessionProperty(indexerUIIDKey, indexerPageID);
	
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param oldIndexerID
	 * @param project
	 */
	public void setIndexerID(String oldIndexerID, IProject project) {
		//Get the corresponding text for the given indexer id
		selectedIndexerId = getIndexerPageName(oldIndexerID);

		//Set the appropriate indexer in the combo box
		indexersComboBox.setText(selectedIndexerId);
		//Load the appropriate page
		setPage();	
		//Give the contributed page a chance to initialize
		if (currentPage instanceof AbstractIndexerPage){
			((AbstractIndexerPage) currentPage).initialize(project);
		}
	}
	
	 
	private static void saveIndexerInfo (String indexerID, String indexerUIID, Element rootElement, Document doc ) {
		
		//Save the indexer id
		Element indexerIDElement = doc.createElement(IndexManager.INDEXER_ID);
		indexerIDElement.setAttribute(IndexManager.INDEXER_ID_VALUE,indexerID);
		rootElement.appendChild(indexerIDElement);
		
		//Save the indexer UI id
		Element indexerUIIDElement = doc.createElement(INDEXER_UI);
		indexerUIIDElement.setAttribute(INDEXER_UI_VALUE,indexerUIID);
		rootElement.appendChild(indexerUIIDElement);
	}
	
	public String getSelectedIndexerID(){
		String indexerID = null;
		
		int selIndex = indexersComboBox.getSelectionIndex();
		
		//If no indexer has been selected return
		if (selIndex != -1)
		 indexerID = indexersComboBox.getItem(selIndex);
		
		return indexerID;
	}
}

Back to the top