DesignMenuModel.java 15.6 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.views;

29 30 31
//import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;

32 33 34 35
import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
36 37 38
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
39 40

import org.eclipse.core.resources.IResource;
41 42 43 44 45 46 47 48 49
//import org.eclipse.jface.viewers.TreeViewer;
//import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
//import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
50 51 52 53 54 55 56 57 58 59

import com.elphel.vdt.VDT;
import com.elphel.vdt.core.tools.ToolsCore;
import com.elphel.vdt.core.tools.config.Config;
import com.elphel.vdt.core.tools.contexts.PackageContext;
import com.elphel.vdt.core.tools.contexts.ProjectContext;
import com.elphel.vdt.core.tools.menu.DesignMenu;
import com.elphel.vdt.core.tools.menu.DesignMenuItem;
import com.elphel.vdt.core.tools.menu.DesignMenuToolItem;
import com.elphel.vdt.core.tools.params.Tool;
Andrey Filippov's avatar
Andrey Filippov committed
60
import com.elphel.vdt.core.tools.params.Tool.TOOL_STATE;
61
import com.elphel.vdt.ui.VDTPluginImages;
62
import com.elphel.vdt.ui.variables.SelectedResourceManager;
63 64 65 66 67 68 69 70 71 72 73 74 75

/**
 * Adapter of core design menu for user interface level.
 * 
 * Created: 28.03.2006
 * @author  Lvov Konstantin
 */

public class DesignMenuModel {

    private Config config;
    
    private MenuItem root;
76
    private final int IMAGE_MARGIN=2;
77 78 79 80 81 82 83 84 85 86 87 88 89
    public DesignMenuModel(DesignMenu menu) {
        config = ToolsCore.getConfig();
        root = new MenuItem(null, menu);
    }
    
    public Item[] getItems() {
        return root.getChildren();  
    }

    // ------------------------------------------------------------------------
    public abstract class Item {
        private static final String ICON_ID_PREFIX = VDT.ID_VDT + ".DesignMenu.Image.";
        private String imageKey = null;
90 91
        
        protected String imageKeyState = null;
92 93 94

        private DesignMenuItem source;
        private Item parent;
95 96 97 98 99 100 101
        private int animN;
        private Rectangle animBounds=null;
        private ImageData[] imageData=null;
//        private boolean animEn=true; // just testing, fill be  false;
        final private AtomicBoolean animBusy=new AtomicBoolean(false);
        private Timer timer=null;
//        private int updateWidth=16;
102 103 104
        private Item(Item parent, DesignMenuItem source) {
            this.parent = parent;
            this.source = source;
105
            this.timer=null;
106 107 108 109 110 111 112 113 114 115 116 117 118

            String image = source.getIcon();
            if (image != null) {
//              imageKey = ICON_ID_PREFIX + source.getName();
                imageKey = ICON_ID_PREFIX + (new File(source.getName())).getName();
                if (VDTPluginImages.getImage(imageKey) == null)
                    VDTPluginImages.addImage(image, imageKey, null/*tool.getLaunchType()*/);
            }
        }

        public boolean isEnabled(IResource resource) {
            return false;
        }
119 120 121
        public boolean isEnabled(String path) {
            return false;
        }
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

        public Object getParent() {
            return parent;
        }

        public Item[] getChildren() {
            return new Item[0];
        }
        public boolean hasChildren() {
            return false;
        }

        public String toString() {
            String label = source.getLabel();
            return label != null ? label : "Unknown";
        }
        
        public String getLabel() {
            return toString();
        }
        public String getImageKey() {
            return imageKey;
        }
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        public void measureItem (Event event){
        	if (imageKeyState==null) return;
        	Image image=VDTPluginImages.getImage(imageKeyState);
        	if (image!=null) event.width+=IMAGE_MARGIN+image.getBounds().width;
        }
        
        public void showStateIcon(Event event, final Tree tree, final TreeItem item){
        	if ((imageKeyState==null) || (item.isDisposed())) return;
        	int x = event.x + event.width + IMAGE_MARGIN;
        	int itemHeight = tree.getItemHeight();
        	imageData=VDTPluginImages.getImageData(imageKeyState);
        	Image frameImage;
        	boolean isAnimation=false;
        	if ((imageData!=null) && (imageData.length>1) && animBusy.compareAndSet(false,true)){
        		isAnimation=true;
        		animN=animN % imageData.length;
        		frameImage = new Image(Display.getDefault(),imageData[animN]);
        	} else {
        		frameImage = VDTPluginImages.getImage(imageKeyState); // do not dispose() !
        	}
        	int imageHeight = frameImage.getBounds().height;
        	int y = event.y + (itemHeight - imageHeight) / 2;
    		event.gc.drawImage(frameImage, x, y);
168
//    		System.out.println("showStateIcon(): "+imageKeyState);
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
        	if (isAnimation){
            	animBounds=frameImage.getBounds();
            	frameImage.dispose();
            	final int fDelayTime=imageData[animN].delayTime*10; // ms
            	animN++;
    			// item does not include our animation on the right of it, so we need to store image location
    			// relative to the top right corner of the item (height does not change)
    			Rectangle r=item.getBounds();
            	animBounds.x=x-(r.x+r.width);
            	animBounds.y=y-(r.y);
            	
            	TimerTask timerTask=new TimerTask() {          
            		@Override
            		public void run() {
           				refresh(item, tree); // threw on shutdown - 
            		}
            	};
        	
            	if (timer==null) timer=new Timer();
            	try {
            		timer.schedule(timerTask, fDelayTime);
            	} catch (IllegalStateException e){
            		System.out.println("DesignMenuModel(): Timer was somewhere canceled, recovering");
            		timer.cancel();
            		timer=new Timer();
            		timer.schedule(timerTask, fDelayTime);
            	}

            	
            	
/*            	
            	timer.schedule(new TimerTask() {          
            		@Override
            		public void run() {
           				refresh(item, tree); // threw on shutdown - 
            		}
            	}, fDelayTime);
*/            	
        	}
        }
        
        public void refresh(final TreeItem item, final Tree tree) {
        	Display.getDefault().syncExec(new Runnable() {
        		public void run() {
        			if (item.isDisposed()) {
            			animBusy.compareAndSet(true, false);
        				return;
        			}
        			Rectangle r=item.getBounds();
//        			System.out.println("animBounds="+animBounds.toString()+" item.getBounds()="+item.getBounds().toString());
        			tree.redraw(
        					animBounds.x+r.x+r.width,
        					animBounds.y+r.y,
        					animBounds.width,
        					animBounds.height,true);
        			animBusy.compareAndSet(true, false);
        		}
        	});
        }
        
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
        public Tool getTool() {
            return null;   
        }

        public Config getConfig() { 
            return null; 
        }

        public PackageContext getPackageContext() { 
            return  null; 
        }

        public ProjectContext getProjectContext() { 
            return  null; 
        }
    } // class Item
    
    // ------------------------------------------------------------------------
    public class ToolItem extends Item {
        private Tool tool;
        
        private ToolItem(Item parent, DesignMenuToolItem source) {
            super(parent, source);
            tool = ToolsCore.getTool(source.getToolName());
        }

        public boolean isEnabled(IResource resource) {
            if (resource == null)
                return false;

            String extensions[] = tool.getExtensions();
            if (extensions == null)
                return true;
            String resourceExt = resource.getFileExtension();
            if (resourceExt == null)
                return false;
            
            for (int i=0; i < extensions.length; i++) {
                if (resourceExt.equalsIgnoreCase(extensions[i]))
                    return true;    
            }
            return false;
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
        } // isEnabled(IResource)

        public boolean isEnabled(String path) {
            if (path == null)
                return false;

            String extensions[] = tool.getExtensions();
            if (extensions == null)
                return true;
            int index=path.indexOf(".");
            if (index<0) return false;
            String resourceExt = path.substring(index+1);
            for (int i=0; i < extensions.length; i++) {
                if (resourceExt.equalsIgnoreCase(extensions[i]))
                    return true;    
            }
            return false;
        } // isEnabled(String)

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        public Tool getTool() {
            return tool;   
        }

        public Config getConfig() { 
            return config; 
        }

        public PackageContext getPackageContext() {
            PackageContext context = tool.getParentPackage();
            if ((context != null) && context.isVisible())
                return context;
            else
                return null;
        }

        public ProjectContext getProjectContext() { 
            ProjectContext context = tool.getParentProject();
            if ((context != null) && context.isVisible())
                return context;
            else
                return null;
        }
313 314 315
        
        public void measureItem (Event event){
        	super.measureItem (event);
316
        	boolean pinned=tool.isPinned() || !SelectedResourceManager.getDefault().isToolsLinked();
317 318 319 320
        	String iconName,key;
        	if (tool.isRunning()){
        		iconName=VDTPluginImages.ICON_TOOLSTATE_RUNNING;
        		key=     VDTPluginImages.KEY_TOOLSTATE_RUNNING;
Andrey Filippov's avatar
Andrey Filippov committed
321 322 323 324 325 326 327 328 329
        	} else if (tool.isWaiting()){
        		iconName=VDTPluginImages.ICON_TOOLSTATE_WAITING;
        		key=     VDTPluginImages.KEY_TOOLSTATE_WAITING;
        	} else if (tool.isAlmostDone()&& (tool.getState()==TOOL_STATE.SUCCESS )){
        		iconName=VDTPluginImages.ICON_TOOLSTATE_ALMOST_GOOD;
        		key=     VDTPluginImages.KEY_TOOLSTATE_ALMOST_GOOD;
        	} else if (tool.isAlmostDone()&& (tool.getState()==TOOL_STATE.UNKNOWN )){
        		iconName=VDTPluginImages.ICON_TOOLSTATE_ALMOST_WTF;
        		key=     VDTPluginImages.KEY_TOOLSTATE_ALMOST_WTF;
330 331 332
        	} else if (tool.isAlmostDone()){ // restoring state, current state new (erased) or failure
        		iconName=VDTPluginImages.ICON_TOOLSTATE_ALMOST_GOOD;
        		key=     VDTPluginImages.KEY_TOOLSTATE_ALMOST_GOOD;
333 334 335 336 337 338 339
        	} else {
        		switch (tool.getState()){
        		case NEW:
        			iconName=VDTPluginImages.ICON_TOOLSTATE_NEW;
        			key= VDTPluginImages.KEY_TOOLSTATE_NEW;
        			break;
        		case FAILURE:
340
        			if (tool.isDirtyOrChanged()){
341 342 343 344 345 346 347 348
        				iconName=VDTPluginImages.ICON_TOOLSTATE_BAD_OLD;
        				key= VDTPluginImages.KEY_TOOLSTATE_BAD_OLD;
        			} else {
        				iconName=VDTPluginImages.ICON_TOOLSTATE_BAD;
        				key= VDTPluginImages.KEY_TOOLSTATE_BAD;
        			}
        			break;
        		case SUCCESS:
349 350 351
        			if (pinned){
    					iconName=VDTPluginImages.ICON_TOOLSTATE_PINNED;
    					key= VDTPluginImages.KEY_TOOLSTATE_PINNED;
352
        			} else {
353
        				if (tool.isDirtyOrChanged()){
354 355 356 357 358 359
        					iconName=VDTPluginImages.ICON_TOOLSTATE_GOOD_OLD;
        					key= VDTPluginImages.KEY_TOOLSTATE_GOOD_OLD;
        				} else {
        					iconName=VDTPluginImages.ICON_TOOLSTATE_GOOD;
        					key= VDTPluginImages.KEY_TOOLSTATE_GOOD;
        				}
360 361
        			}
        			break;
362 363 364 365
        		case KEPT_OPEN:
        				iconName=VDTPluginImages.ICON_TOOLSTATE_KEPT_OPEN;
        				key= VDTPluginImages.KEY_TOOLSTATE_KEPT_OPEN;
        			break;
366
        		default:
367
        			if (tool.isDirtyOrChanged()){
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        				iconName=VDTPluginImages.ICON_TOOLSTATE_WTF_OLD;
        				key= VDTPluginImages.KEY_TOOLSTATE_WTF_OLD;
        			} else {
        				iconName=VDTPluginImages.ICON_TOOLSTATE_WTF;
        				key= VDTPluginImages.KEY_TOOLSTATE_WTF;
        			}
        		}
        	}
            key = Item.ICON_ID_PREFIX + key;
            if (VDTPluginImages.getImage(key) == null){
                VDTPluginImages.addImage(iconName, key, null);
        	}
        	super.imageKeyState=key;
//        	System.out.println("iconName="+iconName+", key="+key+" imageKeyState="+super.imageKeyState);
        	/*        	
        */
//TODO: Save tool state in memento        	
        }
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
    } // class ToolItem

    // ------------------------------------------------------------------------
    public class MenuItem extends Item {
        private Item[] items;
        
        private MenuItem(Item parent, DesignMenu source) {
            super(parent, source);
            
            List<Item> itemList = new ArrayList<Item>();
            
            for(Iterator<DesignMenuItem> i = source.getItems().iterator(); i.hasNext();) {
                DesignMenuItem menuitem = (DesignMenuItem)i.next();

                if(menuitem.isVisible())
                    itemList.add(CreateItem(this, menuitem)); 
            }
        
            items = (Item[])itemList.toArray(new Item[itemList.size()]);
        }    
        
        public Item[] getChildren() {
            return items;
        }
        
        public boolean hasChildren() {
            return items.length > 0;
        }

    } // class MenuItem

    private Item CreateItem(Item parent, DesignMenuItem source) {
        if (source instanceof DesignMenuToolItem)
            return new ToolItem(parent, (DesignMenuToolItem)source);
        else if (source instanceof DesignMenu) 
            return new MenuItem(parent, (DesignMenu)source);
        else
            assert(false);
        return null;
    }
    
} // class DesignMenuModel