JAL-2316 Unit test updates and associated minor changes and fixes.
[jalview.git] / src / jalview / urls / UrlLinkDisplay.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21
22 package jalview.urls;
23
24 import jalview.util.MessageManager;
25 import jalview.util.UrlLink;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 /**
31  * UrlLink table row definition
32  * 
33  * @author $author$
34  * @version $Revision$
35  */
36
37 public class UrlLinkDisplay
38 {
39   private String id; // id is not supplied to display, but used to identify
40                      // entries when saved
41
42   private boolean isDefault;
43
44   private boolean isSelected;
45
46   private UrlLink link;
47
48   // Headers for columns in table
49   private final static List<String> colNames = new ArrayList<String>()
50   {
51     {
52       add(MessageManager.formatMessage("label.name"));
53       add(MessageManager.formatMessage("label.url"));
54       add(MessageManager.formatMessage("label.inmenu"));
55       add(MessageManager.formatMessage("label.default"));
56       add(MessageManager.formatMessage("label.id"));
57     }
58   };
59
60   // column positions
61   public final static int NAME = 0;
62
63   public final static int URL = 1;
64
65   public final static int SELECTED = 2;
66
67   public final static int DEFAULT = 3;
68
69   public final static int ID = 4;
70
71   public UrlLinkDisplay(String rowId, UrlLink rowLink,
72           boolean rowSelected, boolean rowDefault)
73   {
74     id = rowId;
75     isDefault = rowDefault;
76     isSelected = rowSelected;
77
78     link = rowLink;
79   }
80
81   // getters/setters
82   public String getId()
83   {
84     return id;
85   }
86
87   public String getName()
88   {
89     return link.getLabel();
90   }
91
92   public String getUrl()
93   {
94     return link.getUrlWithToken();
95   }
96
97   public boolean getIsDefault()
98   {
99     return isDefault;
100   }
101
102   public boolean getIsSelected()
103   {
104     return isSelected;
105   }
106
107   public void setName(String name)
108   {
109     link.setLabel(name);
110   }
111
112   public void setUrl(String rowUrl)
113   {
114     link = new UrlLink(rowUrl);
115   }
116
117   public void setIsDefault(boolean rowDefault)
118   {
119     isDefault = rowDefault;
120   }
121
122   public void setIsSelected(boolean rowSelected)
123   {
124     isSelected = rowSelected;
125   }
126
127   public Object getValue(int index)
128   {
129     switch (index)
130     {
131     case ID:
132       return id;
133     case URL:
134       return getUrl();
135     case DEFAULT:
136       return isDefault;
137     case SELECTED:
138       return isSelected;
139     case NAME:
140       return getName();
141     default:
142       return null;
143     }
144   }
145
146   public void setValue(int index, Object value)
147   {
148     switch (index)
149     {
150     case ID:
151       id = (String) value;
152       break;
153     case URL:
154       setUrl((String) value);
155       break;
156     case DEFAULT:
157       isDefault = (boolean) value;
158       break;
159     case SELECTED:
160       isSelected = (boolean) value;
161       break;
162     case NAME:
163       setName((String) value);
164       break;
165     default:
166       // do nothing
167     }
168   }
169
170   /**
171    * Identify editable columns
172    * 
173    * @param index
174    *          index of column
175    * @return whether column can be edited in table
176    */
177   public boolean isEditable(int index)
178   {
179     if (index == DEFAULT)
180     {
181       // default link must be a $SEQUENCE_ID$ link
182       // so only allow editing if it is
183       return (!link.usesDBAccession());
184     }
185     else if (index == SELECTED)
186     {
187       return true;
188     }
189     else
190     {
191       return false;
192     }
193   }
194
195   /**
196    * Get list of column names to display in UI
197    * 
198    * @return column names
199    */
200   public static List<String> getDisplayColumnNames()
201   {
202     // Display names between NAME and ID (excludes ID)
203     return colNames.subList(NAME, ID);
204   }
205 }