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