SelectedResourceManager.java 16.5 KB
Newer Older
1 2 3
/*******************************************************************************
 * Copyright (c) 2014 Elphel, Inc.
 * Copyright (c) 2006 Elphel, Inc and Excelsior, LLC.
4 5
 * This file is a part of VDT plug-in.
 * VDT plug-in is free software; you can redistribute it and/or modify
6 7 8 9
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
10
 * VDT plug-in is distributed in the hope that it will be useful,
11 12 13 14 15 16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Andrey Filippov's avatar
Andrey Filippov committed
17 18 19 20 21 22 23 24 25
 * 
 *  Additional permission under GNU GPL version 3 section 7:
 * If you modify this Program, or any covered work, by linking or combining it
 * with Eclipse or Eclipse plugins (or a modified version of those libraries),
 * containing parts covered by the terms of EPL/CPL, the licensors of this
 * Program grant you additional permission to convey the resulting work.
 * {Corresponding Source for a non-source form of such a combination shall
 * include the source code for the parts of Eclipse or Eclipse plugins used
 * as well as that of the covered work.}
26
 *******************************************************************************/
27 28
package com.elphel.vdt.ui.variables;

29
import java.text.ParseException;
30 31
import java.text.SimpleDateFormat;
import java.util.Date;
32
import java.util.Stack;
Andrey Filippov's avatar
Andrey Filippov committed
33

34
import com.elphel.vdt.VDT;
35
import com.elphel.vdt.VerilogUtils;
36
import com.elphel.vdt.core.tools.params.Tool;
37
import com.elphel.vdt.core.tools.params.ToolSequence;
Andrey Filippov's avatar
Andrey Filippov committed
38
import com.elphel.vdt.ui.MessageUI;
39 40
import com.elphel.vdt.veditor.VerilogPlugin;
import com.elphel.vdt.veditor.preference.PreferenceStrings;
Andrey Filippov's avatar
Andrey Filippov committed
41

42
import org.eclipse.core.resources.IContainer;
43 44 45
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
46
import org.eclipse.core.resources.ResourcesPlugin;
47
import org.eclipse.core.runtime.IAdaptable;
48 49
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
50 51 52 53 54
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
55
import org.eclipse.ui.IMemento;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWindowListener;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

/**
 * Maintains the context used to launch tools. The context is based on
 * the selected resource.
 * 
 * Created: 26.01.2006
 * @author  Lvov Konstantin
 */

public class SelectedResourceManager implements IWindowListener, ISelectionListener {
76

77 78 79 80 81 82 83
    // singleton
    private static SelectedResourceManager fgDefault = new SelectedResourceManager();
    private IResource fSelectedResource    = null;
    private IResource fSelectedVerilogFile = null;
    private ITextSelection fSelectedText = null;
    private Stack<IWorkbenchWindow> fWindowStack = new Stack<IWorkbenchWindow>();

84 85
    //Andrey
    private String fChosenTarget=null; // full path of the chosen (for action) resource or any string. Used to calculate CurrentFile, verilog file, ...
86
    private String fChosenShort=null; // last segment of the chosen resource name
87 88
    private IResource fChosenVerilogFile = null; // to keep fSelectedVerilogFile
    private int fChosenAction=0; // Chosen variant of running the tool
89 90
//    private long timestamp=0;
    private String timestamp;
91
    private String ignoreFilter=null;
92
    private boolean toolsLinked=true;
93
    private ToolSequence toolSequence=null; // to be able to reach toolSequence instance from VEditor
94
    
95
//    private Tool selectedTool=null; // last selected tool
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
    private SelectedResourceManager() {
        IWorkbench workbench = PlatformUI.getWorkbench();
        if (workbench != null) { //may be running headless
            workbench.addWindowListener(this);
            IWorkbenchWindow activeWindow = workbench.getActiveWorkbenchWindow();
            if (activeWindow != null) {
                windowActivated(activeWindow);
            }
        } 
    } // SelectedResourceManager()
    
    /**
     * Returns the singleton resource selection manager
     * 
     * @return VariableContextManager
     */
    public static SelectedResourceManager getDefault() {
    	if (fgDefault.fSelectedResource == null)
    		fgDefault.tryDefaultSelection();
        return fgDefault;
    }

    private void tryDefaultSelection() {
        IWorkbenchPage page = fWindowStack.peek().getActivePage();
        if (page != null) {
            IViewPart view = page.findView(IPageLayout.ID_RES_NAV);
            if (view != null)
            	selectionChanged(view, view.getViewSite().getSelectionProvider().getSelection());
        }
    }
    
128 129 130 131 132 133 134
    public void setToolsLinked(boolean linked){
    	this.toolsLinked=linked;
    }
    public boolean isToolsLinked(){
    	return toolsLinked;
    }
    
135 136 137 138 139 140 141 142
    public void setToolSequence (ToolSequence toolSequence){
    	this.toolSequence=toolSequence;
    }

    public ToolSequence getToolSequence(){
    	return toolSequence;
    }
    
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
    /**
     * @see org.eclipse.ui.IWindowListener#windowActivated(org.eclipse.ui.IWorkbenchWindow)
     */
    public void windowActivated(IWorkbenchWindow window) {
        fWindowStack.remove(window);
        fWindowStack.push(window);
        ISelectionService service = window.getSelectionService(); 
        service.addSelectionListener(this);
        IWorkbenchPage page = window.getActivePage();
        if (page != null) {
            IWorkbenchPart part = page.getActivePart();
            if (part != null) {             
                ISelection selection = service.getSelection();
                if (selection != null) {
                    selectionChanged(part, selection);
                }
            }
        }
    } // windowActivated()

    /**
     * @see org.eclipse.ui.IWindowListener#windowClosed(org.eclipse.ui.IWorkbenchWindow)
     */
    public void windowClosed(IWorkbenchWindow window) {
        ISelectionService selectionService = window.getSelectionService();
        selectionService.removeSelectionListener(this);
        fWindowStack.remove(window);
    }

    /**
     * @see org.eclipse.ui.IWindowListener#windowDeactivated(org.eclipse.ui.IWorkbenchWindow)
     */
    public void windowDeactivated(IWorkbenchWindow window) {
    }

    /**
     * @see org.eclipse.ui.IWindowListener#windowOpened(org.eclipse.ui.IWorkbenchWindow)
     */
    public void windowOpened(IWorkbenchWindow window) {
        windowActivated(window);
    }

    /**
     * Returns the currently selected resource in the active workbench window,
187
     * or <code>null</code> if none. If an editor is active, the resource adapter
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
     * associated with the editor is returned.
     * 
     * @return selected resource or <code>null</code>
     */
    public IResource getSelectedResource() {
        return fSelectedResource;
    }
    
    public IProject getSelectedProject() {
        if (fSelectedResource != null)
            return fSelectedResource.getProject();
        else
            return null;
    }

    public IResource getSelectedVerilogFile() {
        return fSelectedVerilogFile;
    }

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
     * Returns resource by selection in the active workbench window,
     * or <code>null</code> if none. If an editor is active, the resource adapater
     * associated with the editor is returned.
     * 
     * @return selected resource or <code>null</code>
     */
    public IResource getSelectedResource(IWorkbenchPart part, ISelection selection) {
        IResource selectedResource = null;
        if (selection instanceof IStructuredSelection) {
            Object result = ((IStructuredSelection)selection).getFirstElement();
            if (result instanceof IResource) {
                selectedResource = (IResource) result;
            } else if (result instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) result;
                selectedResource = (IResource)adaptable.getAdapter(IResource.class);
            }
        }
        
        if (selectedResource == null) {
            // If the active part is an editor, get the file resource used as input.
            if (part instanceof IEditorPart) {
                IEditorPart editorPart = (IEditorPart) part;
                IEditorInput input = editorPart.getEditorInput();
                selectedResource = (IResource) input.getAdapter(IResource.class);
            } 
        }

        if (selectedResource != null)
            return selectedResource;
        else
            return fSelectedResource;
    } // getSelectedResource()
    

    public IResource getViewSelectedResource(String viewId) {
        IWorkbenchPage page = fWindowStack.peek().getActivePage();
        if (page != null) {
            IViewPart view = page.findView(viewId);
            if (view != null) {
                return getSelectedResource(view, view.getViewSite().getSelectionProvider().getSelection());
            }    
        }
        return null;
    }
    
    /**
     * @see org.eclipse.ui.ISelectionListener#selectionChanged(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
     */
    public void selectionChanged(IWorkbenchPart part, ISelection selection) {
257 258
		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_OTHER))
			System.out.print("$$$ Selection changed, fSelectedResource="+fSelectedResource);
259
//    	System.out.println("SelectedResourceManager.selectionChanged()");
260 261 262
        IWorkbenchWindow window = part.getSite().getWorkbenchWindow();
        if (fWindowStack.isEmpty() || !fWindowStack.peek().equals(window)) {
            // selection is not in the active window
263
        	System.out.println(" - stray selection outside acrtive window");
264 265 266
            return;
        }
        IResource selectedResource = getSelectedResource(part, selection);
267 268
		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_OTHER))
			System.out.println(" New selection: "+selectedResource);
269 270
        if (selectedResource != null) {
            fSelectedResource = selectedResource;
271
            if ((selectedResource.getType()==IResource.FILE) && (VerilogUtils.isHhdlFile((IFile)fSelectedResource))){
272
                fSelectedVerilogFile = selectedResource; /* Maybe same will work for vhdl too? */
273 274 275
        		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_OTHER))
        			System.out.println("Updated fSelectedVerilogFile: "+fSelectedVerilogFile);
            } else {
Andrey Filippov's avatar
Andrey Filippov committed
276 277
        		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_OTHER))
        			System.out.println(selectedResource+" is not a file or not an HDL file");
278
            }
279 280 281 282 283 284
        }
        
        if (selection instanceof ITextSelection) {
            fSelectedText = (ITextSelection)selection;
        }
    } // selectionChanged()
285 286 287 288 289 290 291 292 293 294
//TODO: Make them project-relative 
    public String tryRelativePath(String path){
    	if (path==null)
    		return null;
    	if (getSelectedProject()==null) return path;
    	IProject project=getSelectedProject();
    	if (path.startsWith(project.getLocation().toString()))
    		return path.substring(project.getLocation().toString().length()+1);
    	return path;
    }
295
    public void updateActionChoice(String chosenTarget, int choice, String ignoreFilter){
296 297
    	fChosenAction=choice;
    	fChosenTarget=tryRelativePath(chosenTarget);
298
    	this.ignoreFilter=ignoreFilter;
299 300 301
    	IProject project=getSelectedProject();
    	if (project==null) return;
    	IPath path = new Path(fChosenTarget);
302 303 304 305 306 307
    	IFile file=null;
    	try {
    		file = (path==null)?null:project.getFile(path);
    	} catch (IllegalArgumentException e) {
    		// Path must include project and resource name: /x353
    	}
308 309 310 311
    	if ((file != null) &&  (VerilogUtils.isHhdlFile(file)))
    		fChosenVerilogFile=file;
    	else if (fChosenVerilogFile==null)
    		fChosenVerilogFile=fSelectedVerilogFile;
312 313 314 315 316
    	if (file!=null){
    		fChosenShort=file.getName(); // last segment
    	} else {
    		fChosenShort=fChosenTarget; // whatever
    	}
317
    }
318
    
319 320
    
    // Build stamp/date methods
321
    public String setBuildStamp(){
322
    	timestamp=getBuildStamp(new Date());
323 324 325 326
    	return getBuildStamp();
    }

    public String getBuildStamp(){
327
    	return timestamp;
328
    }
329 330 331 332

    public static String getBuildStamp(Date date){
    	return new SimpleDateFormat(VDT.TIME_STAMP_FORMAT).format(date);
    }
333
    
334 335 336 337 338 339 340 341 342 343 344 345 346 347
    public static Date parseStamp(String stamp){
    	Date d;
		try {
			d = new SimpleDateFormat(VDT.TIME_STAMP_FORMAT).parse(stamp);
		} catch (ParseException e) {
			d=new Date(0);
			System.out.println("Date format '"+stamp+"' not recognized, using beginning of all of times: "+
					new SimpleDateFormat(VDT.TIME_STAMP_FORMAT).format(d));
			return d; // 1970
		}
    	return d;
    }
    
    public static boolean afterStamp(String after, String before){
Andrey Filippov's avatar
Andrey Filippov committed
348
    	return  parseStamp(after).after(parseStamp(before));
349
    }
350
    
351 352 353
    public String getChosenTarget() {
        return fChosenTarget;
    }
354 355 356
    public String getChosenShort() {
        return fChosenShort;
    }
357
    
358
    public IResource getChosenVerilogFile() {
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    	IProject project=getSelectedProject();
    	if (project==null) return null;
    	IResource rslt=(fChosenVerilogFile!=null)?fChosenVerilogFile:fSelectedVerilogFile;
    	if (rslt==null) return null;
    	if (project.getFullPath().toPortableString().equals(rslt.getProject().getFullPath().toPortableString())){
    		return (fChosenVerilogFile!=null)?fChosenVerilogFile:fSelectedVerilogFile;
    	} else {
    		System.out.println("Wrong getChosenVerilogFile="+rslt+" for project "+project);
    		if (fSelectedVerilogFile==null) return null;
    		if (project.getFullPath().toPortableString().equals(fSelectedVerilogFile.getProject().getFullPath().toPortableString())){
        		System.out.println("Using: "+fSelectedVerilogFile);
    			return  fSelectedVerilogFile;
    		} else {
        		System.out.println("fSelectedVerilogFile is also wrong: "+fSelectedVerilogFile);
    		}
    		return fSelectedResource;
    	}
376
    }
377 378

    // Used when restoring from memento
Andrey Filippov's avatar
Andrey Filippov committed
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    public void setChosenVerilogFile(IResource file) {
        if (file != null){
        	if (file.getType() != IResource.FILE) {
        		String msg="Tried to use "+file+" as HDL file";
        		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_OTHER)) {
        			MessageUI.error(msg);
        		}
        		System.out.println(msg);
        		return;
        	}
        	if (!VerilogUtils.isHhdlFile((IFile)file)){
        		String msg="Tried to use non-HDL "+file+" as HDL file";
        		if (VerilogPlugin.getPreferenceBoolean(PreferenceStrings.DEBUG_OTHER)) {
        			MessageUI.error(msg);
        		}
        		System.out.println(msg);
        		return;
        	}

        }
399
    	fChosenVerilogFile=file;
400 401 402 403 404 405
    	IProject project=getSelectedProject();
    	IProject newProject= (file == null)? null: file.getProject();
    	// if file is different project than selectedResource
    	if ((newProject != null) && ((project == null) || !newProject.getFullPath().toPortableString().equals(project.getFullPath().toPortableString()))){
    		fSelectedResource=file;
    	}
406 407 408
    	if (fSelectedResource==null) fSelectedResource=file;
    }

409 410 411 412
    
    public int getChosenAction() {
        return fChosenAction;
    }
413 414 415 416 417 418 419 420
    
 //   public Tool getSelectedTool(){
 //   	return selectedTool;
 //   }
    
    public String getFilter(){
    	return ignoreFilter;
    }
421 422 423
    public void setFilter(String  filter){
    	ignoreFilter=filter;
    }
424 425


426 427 428 429 430 431 432 433 434 435 436 437 438
    
    
    /**
     * Returns the current text selection as a <code>String</code>, or <code>null</code> if
     * none.
     * 
     * @return the current text selection as a <code>String</code> or <code>null</code>
     */
    public String getSelectedText() {
        return fSelectedText.getText();
    }
    
} // class SelectedResourceManager()