1 /* 2 * Copyright (c) 2006 Stiftung Deutsches Elektronen-Synchroton, 3 * Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY. 4 * 5 * THIS SOFTWARE IS PROVIDED UNDER THIS LICENSE ON AN "../AS IS" BASIS. 6 * WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED 7 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR PARTICULAR PURPOSE AND 8 * NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 9 * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 10 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR 11 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. SHOULD THE SOFTWARE PROVE DEFECTIVE 12 * IN ANY RESPECT, THE USER ASSUMES THE COST OF ANY NECESSARY SERVICING, REPAIR OR 13 * CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. 14 * NO USE OF ANY SOFTWARE IS AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER. 15 * DESY HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, 16 * OR MODIFICATIONS. 17 * THE FULL LICENSE SPECIFYING FOR THE SOFTWARE THE REDISTRIBUTION, MODIFICATION, 18 * USAGE AND OTHER RIGHTS AND OBLIGATIONS IS INCLUDED WITH THE DISTRIBUTION OF THIS 19 * PROJECT IN THE FILE LICENSE.HTML. IF THE LICENSE IS NOT INCLUDED YOU MAY FIND A COPY 20 * AT HTTP://WWW.DESY.DE/LEGAL/LICENSE.HTM 21 */ 22 23 package de.desy.acop.demo.example; 24 25 import java.awt.Color; 26 import java.beans.PropertyVetoException; 27 import java.io.File; 28 29 import com.cosylab.gui.ValueIconPair; 30 import com.cosylab.gui.components.util.IconHelper; 31 import com.cosylab.util.CommonException; 32 33 import de.desy.acop.displayers.AcopIcon; 34 import de.desy.acop.transport.AccessMode; 35 import de.desy.acop.transport.ConnectionParameters; 36 37 public class IconExample { 38 39 public static void main(String[] args) { 40 41 //How to connect icon to a remote property? 42 //First we create an instance of the AcopIcon object. 43 AcopIcon icon = new AcopIcon(); 44 //We require ConnectionParameters which describe the remote property 45 ConnectionParameters parameters = new ConnectionParameters("TINE", 46 "context", "server", "device", "property", AccessMode.POLL, 1000); 47 //These parameters describe a remote property 'context/server/device/property' 48 //in the TINE control system. We will request a constant monitoring, 49 //therefore AccessMode.POLL was used. Polling rate is set to 1000 milliseconds, 50 //which means that value on the icon will be updated every 1000 milliseconds. 51 //Parameters are then attached to the icon. Connection is created automatically. 52 try { 53 icon.setConnectionParameters(parameters); 54 } catch (CommonException e) { 55 e.printStackTrace(); 56 } catch (PropertyVetoException e) { 57 e.printStackTrace(); 58 } 59 //If no exception occurred, icon should show the remote value rendered as an appropriate icon. 60 //The icons used to render the values received can be set as 61 ValueIconPair[] valueIconPairs = new ValueIconPair[] { 62 new ValueIconPair(0L, IconHelper.createIcon("icons/navigation/ArrowLeftLeft16.gif")), 63 new ValueIconPair(1L, IconHelper.createIcon("icons/navigation/ArrowLeft16.gif")), 64 new ValueIconPair(2L, IconHelper.createIcon("icons/navigation/ArrowRight16.gif")), 65 new ValueIconPair(3L, IconHelper.createIcon("icons/navigation/ArrowRightRight16.gif")) 66 }; 67 icon.setIcons(valueIconPairs); 68 //Value icon pairs can be added also later or in the runtime 69 icon.addIcon(new ValueIconPair(4L, IconHelper.createIcon("icons/navigation/Plus16.gif"))); 70 //When using the IconHelper there are several ways how to create an icon. 71 //The above code, where string parameters are supplied will search for the given 72 //path in the classpath of the project and create an icon if the path exists. 73 //If we want to use a local image file for the icon, we can generate the File 74 //pointing to that location and pass it to the IconHelper 75 File file = new File("C:/foo.png"); 76 ValueIconPair pair = new ValueIconPair(5L, file); 77 icon.addIcon(pair); 78 79 80 81 //We want to be notified when value is out of bounds. In Icon displayer this 82 //means that a displayer will be updated with a value, which doesn't have any 83 //icon assigned to (the ValueIconPair for that value was never added). Therefore, 84 //we set a default icon to the IconDisplayer, which will be shown in these cases: 85 icon.setDefaultIcon(IconHelper.createIcon("icons/media/Stop16.gif")); 86 87 //All icons are not the same size as the component is, which can result 88 //in only a part of the icon being visible in the displayer, or only a part 89 //of the displayer is filled with icon. We can force the displayer to scale 90 //all icons to fit its size 91 icon.setScaleIcons(true); 92 //The icons will now be the size of the displayer but they might look strange 93 //because the displayer does not have the aspect ration of the icon. Therefore, 94 //we will set the displayer to preserve the aspect ratio of icons. 95 icon.setKeepRatio(true); 96 97 //Other properties of the icon can also simply be adjusted by calling the 98 //appropriate setter methods. These properties include background/foreground color, title etc.: 99 icon.setBackground(Color.RED); 100 icon.setForeground(Color.GREEN); 101 } 102 }