814370eddbcf827d916c023f1c4d6565968e5fbe
[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 isPrimary;
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.database"));
53       add(MessageManager.formatMessage("label.name"));
54       add(MessageManager.formatMessage("label.url"));
55       add(MessageManager.formatMessage("label.inmenu"));
56       add(MessageManager.formatMessage("label.primary"));
57       add(MessageManager.formatMessage("label.id"));
58     }
59   };
60
61   // column positions
62   public final static int DATABASE = 0;
63
64   public final static int NAME = 1;
65
66   public final static int URL = 2;
67
68   public final static int SELECTED = 3;
69
70   public final static int PRIMARY = 4;
71
72   public final static int ID = 5;
73
74   public UrlLinkDisplay(String rowId, UrlLink rowLink,
75           boolean rowSelected, boolean rowDefault)
76   {
77     id = rowId;
78     isPrimary = rowDefault;
79     isSelected = rowSelected;
80
81     link = rowLink;
82   }
83
84   // getters/setters
85   public String getId()
86   {
87     return id;
88   }
89
90   public String getName()
91   {
92     return link.getLabel();
93   }
94
95   public String getDBName()
96   {
97     return link.getDBName();
98   }
99
100   public String getUrl()
101   {
102     return link.getUrlWithToken();
103   }
104
105   public boolean getIsPrimary()
106   {
107     return isPrimary;
108   }
109
110   public boolean getIsSelected()
111   {
112     return isSelected;
113   }
114
115   public void setName(String name)
116   {
117     link.setLabel(name);
118   }
119
120   public void setUrl(String rowUrl)
121   {
122     link = new UrlLink(getName(), rowUrl, getName());
123   }
124
125   public void setDescription(String desc)
126   {
127     link.setDescription(desc);
128   }
129
130   public void setIsDefault(boolean rowDefault)
131   {
132     isPrimary = rowDefault;
133   }
134
135   public void setIsSelected(boolean rowSelected)
136   {
137     isSelected = rowSelected;
138   }
139
140   public Object getValue(int index)
141   {
142     switch (index)
143     {
144     case ID:
145       return id;
146     case URL:
147       return getUrl();
148     case PRIMARY:
149       return isPrimary;
150     case SELECTED:
151       return isSelected;
152     case NAME:
153       return getName();
154     case DATABASE:
155       return getDBName();
156     default:
157       return null;
158     }
159   }
160
161   public void setValue(int index, Object value)
162   {
163     switch (index)
164     {
165     case ID:
166       id = (String) value;
167       break;
168     case URL:
169       setUrl((String) value);
170       break;
171     case PRIMARY:
172       isPrimary = (boolean) value;
173       break;
174     case SELECTED:
175       isSelected = (boolean) value;
176       break;
177     case NAME:
178       setDescription((String) value);
179     case DATABASE:
180       setName((String) value);
181       break;
182     default:
183       // do nothing
184     }
185   }
186
187   /**
188    * Identify editable columns
189    * 
190    * @param index
191    *          index of column
192    * @return whether column can be edited in table
193    */
194   public boolean isEditable(int index)
195   {
196     if (index == PRIMARY)
197     {
198       // primary link must not be a $DB_ACCESSION$ link
199       // so only allow editing if it is not
200       return (!link.usesDBAccession());
201     }
202     else if (index == SELECTED)
203     {
204       return true;
205     }
206     else
207     {
208       return false;
209     }
210   }
211
212   /**
213    * Get list of column names to display in UI
214    * 
215    * @return column names
216    */
217   public static List<String> getDisplayColumnNames()
218   {
219     // Display names between DESCRIPTION and ID (excludes ID)
220     return colNames.subList(DATABASE, ID);
221   }
222 }