Source code: com/virtuosotechnologies/asaph/standardgui/InfoPane.java
1 /*
2 ================================================================================
3
4 FILE: InfoPane.java
5
6 PROJECT:
7
8 Asaph
9
10 CONTENTS:
11
12 A pane that displays informational fields for a song.
13
14 PROGRAMMERS:
15
16 Daniel Azuma (DA) <dazuma@kagi.com>
17
18 COPYRIGHT:
19
20 Copyright (C) 2003 Daniel Azuma (dazuma@kagi.com)
21
22 This program is free software; you can redistribute it and/or
23 modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2
25 of the License, or (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU General Public License for more details.
31
32 You should have received a copy of the GNU General Public
33 License along with this program; if not, write to
34 Free Software Foundation, Inc.
35 59 Temple Place, Suite 330
36 Boston, MA 02111-1307 USA
37
38 ================================================================================
39 */
40
41
42 package com.virtuosotechnologies.asaph.standardgui;
43
44
45 import java.awt.BorderLayout;
46 import java.awt.Dimension;
47 import java.awt.GridBagConstraints;
48 import java.awt.GridBagLayout;
49 import java.awt.Insets;
50 import java.awt.Color;
51 import java.awt.event.ComponentAdapter;
52 import java.awt.event.ComponentEvent;
53 import java.util.prefs.Preferences;
54 import javax.swing.JScrollPane;
55 import javax.swing.JPanel;
56 import javax.swing.JLabel;
57 import javax.swing.JTextArea;
58 import javax.swing.JComponent;
59 import javax.swing.BorderFactory;
60
61 import com.virtuosotechnologies.lib.swing.ScrollablePanel;
62
63 import com.virtuosotechnologies.asaph.model.Song;
64 import com.virtuosotechnologies.asaph.modelutils.SongUtils;
65
66
67 /**
68 * A pane that displays informational fields for a song.
69 */
70 /*package*/ class InfoPane
71 {
72 private static final String INFOPANE_WIDTH_PREF = "infoPaneWidth";
73
74 private static final String STR_InfoPane_MainTitleLabel =
75 ResourceAccess.Strings.buildString("InfoPane_MainTitleLabel");
76 private static final String STR_InfoPane_AltTitlesLabel =
77 ResourceAccess.Strings.buildString("InfoPane_AltTitlesLabel");
78 private static final String STR_InfoPane_KeywordsLabel =
79 ResourceAccess.Strings.buildString("InfoPane_KeywordsLabel");
80 private static final String STR_InfoPane_AuthorLabel =
81 ResourceAccess.Strings.buildString("InfoPane_AuthorLabel");
82 private static final String STR_InfoPane_CopyrightLabel =
83 ResourceAccess.Strings.buildString("InfoPane_CopyrightLabel");
84 private static final String STR_InfoPane_NotesLabel =
85 ResourceAccess.Strings.buildString("InfoPane_NotesLabel");
86 private static final String STR_InfoPane_CommentLabel =
87 ResourceAccess.Strings.buildString("InfoPane_CommentLabel");
88
89 private static Preferences prefs_;
90 static
91 {
92 prefs_ = Preferences.userNodeForPackage(InfoPane.class);
93 }
94
95
96 private JScrollPane toplevelPane_;
97 private int widthCorrection_ = Integer.MIN_VALUE;
98
99
100 /**
101 * Constructor
102 */
103 /*package*/ InfoPane(
104 Song song,
105 SongUtils songUtils)
106 {
107 JPanel mainPanel = new JPanel(new GridBagLayout());
108 mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
109 GridBagConstraints gbc = new GridBagConstraints();
110 gbc.weighty = 1;
111 gbc.gridy = 0;
112
113 createStringView(STR_InfoPane_MainTitleLabel,
114 songUtils.getFieldValueAsString(song, Song.MAINTITLE_FIELD),
115 gbc, mainPanel);
116
117 createStringView(STR_InfoPane_CommentLabel,
118 songUtils.getFieldValueAsString(song, Song.COMMENT_FIELD),
119 gbc, mainPanel);
120
121 createStringListView(STR_InfoPane_AltTitlesLabel,
122 songUtils.getFieldValueAsStringArray(song, Song.ALTERNATETITLELIST_FIELD),
123 gbc, mainPanel);
124
125 createStringView(STR_InfoPane_AuthorLabel,
126 songUtils.getFieldValueAsString(song, Song.AUTHOR_FIELD),
127 gbc, mainPanel);
128
129 createStringView(STR_InfoPane_CopyrightLabel,
130 songUtils.getFieldValueAsString(song, Song.COPYRIGHT_FIELD),
131 gbc, mainPanel);
132
133 createStringListView(STR_InfoPane_KeywordsLabel,
134 songUtils.getFieldValueAsStringArray(song, Song.KEYWORDLIST_FIELD),
135 gbc, mainPanel);
136
137 createStringView(STR_InfoPane_NotesLabel,
138 songUtils.getFieldValueAsString(song, Song.NOTESLIST_FIELD),
139 gbc, mainPanel);
140
141 ScrollablePanel viewport = new ScrollablePanel(new BorderLayout());
142 viewport.setScrollableTracksViewportWidth(true);
143 viewport.add(mainPanel, BorderLayout.NORTH);
144 toplevelPane_ = new JScrollPane(viewport,
145 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
146 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
147 final int initialWidth = Math.max(prefs_.getInt(INFOPANE_WIDTH_PREF, 300), 200);
148 toplevelPane_.setPreferredSize(new Dimension(initialWidth, 0));
149 toplevelPane_.addComponentListener(
150 new ComponentAdapter()
151 {
152 public void componentResized(
153 ComponentEvent ev)
154 {
155 int width = toplevelPane_.getWidth();
156 if (widthCorrection_ == Integer.MIN_VALUE)
157 {
158 widthCorrection_ = initialWidth-width;
159 }
160 prefs_.putInt(INFOPANE_WIDTH_PREF, width+widthCorrection_);
161 }
162 });
163 }
164
165
166 private void createStringListView(
167 String labelText,
168 String[] array,
169 GridBagConstraints gbc,
170 JPanel mainPanel)
171 {
172 if (array.length == 0)
173 {
174 return;
175 }
176
177 JLabel label = new JLabel(labelText);
178 gbc.weightx = 0;
179 gbc.anchor = GridBagConstraints.EAST;
180 gbc.fill = GridBagConstraints.NONE;
181 gbc.gridx = 0;
182 gbc.insets = new Insets(5, 0, 0, 5);
183 mainPanel.add(label, gbc);
184
185 gbc.weightx = 1;
186 gbc.anchor = GridBagConstraints.WEST;
187 gbc.fill = GridBagConstraints.HORIZONTAL;
188 gbc.gridx = 1;
189 gbc.insets = new Insets(5, 0, 0, 0);
190 boolean addedOne = false;
191 for (int i=0; i<array.length; ++i)
192 {
193 JTextArea field = new JTextArea(array[i]);
194 field.setEditable(false);
195 field.setBorder(BorderFactory.createLineBorder(Color.black));
196 field.setLineWrap(true);
197 field.setWrapStyleWord(true);
198 mainPanel.add(field, gbc);
199 ++gbc.gridy;
200 if (!addedOne)
201 {
202 addedOne = true;
203 gbc.insets = new Insets(0, 0, 0, 0);
204 }
205 }
206 if (!addedOne)
207 {
208 ++gbc.gridy;
209 }
210 }
211
212
213 private void createStringView(
214 String labelText,
215 String str,
216 GridBagConstraints gbc,
217 JPanel mainPanel)
218 {
219 JLabel label = new JLabel(labelText);
220 gbc.weightx = 0;
221 gbc.anchor = GridBagConstraints.EAST;
222 gbc.fill = GridBagConstraints.NONE;
223 gbc.gridx = 0;
224 gbc.insets = new Insets(5, 0, 0, 5);
225 mainPanel.add(label, gbc);
226
227 JTextArea field = new JTextArea(str);
228 field.setEditable(false);
229 field.setLineWrap(true);
230 field.setWrapStyleWord(true);
231 field.setBorder(BorderFactory.createLineBorder(Color.black));
232 gbc.weightx = 1;
233 gbc.anchor = GridBagConstraints.WEST;
234 gbc.fill = GridBagConstraints.HORIZONTAL;
235 gbc.gridx = 1;
236 gbc.insets = new Insets(5, 0, 0, 0);
237 mainPanel.add(field, gbc);
238
239 ++gbc.gridy;
240 }
241
242
243 /*package*/ JComponent getJComponent()
244 {
245 return toplevelPane_;
246 }
247 }