/[svn]/jsampler/trunk/src/org/jsampler/view/fantasia/MainPane.java
ViewVC logotype

Annotation of /jsampler/trunk/src/org/jsampler/view/fantasia/MainPane.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2288 - (hide annotations) (download)
Wed Nov 23 21:19:44 2011 UTC (12 years, 5 months ago) by iliev
File size: 11066 byte(s)
* Added option to select a sampler engine in Add/Edit Instrument dialog
* Moved all Swing dependent code outside the JSampler core

1 iliev 1286 /*
2     * JSampler - a java front-end for LinuxSampler
3     *
4 iliev 2288 * Copyright (C) 2005-2011 Grigor Iliev <grigor@grigoriliev.com>
5 iliev 1286 *
6     * This file is part of JSampler.
7     *
8     * JSampler is free software; you can redistribute it and/or modify
9     * it under the terms of the GNU General Public License version 2
10     * as published by the Free Software Foundation.
11     *
12     * JSampler is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with JSampler; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20     * MA 02111-1307 USA
21     */
22    
23     package org.jsampler.view.fantasia;
24    
25     import java.awt.Cursor;
26     import java.awt.Dimension;
27     import java.awt.Graphics;
28     import java.awt.GridBagConstraints;
29     import java.awt.GridBagLayout;
30     import java.awt.Insets;
31 iliev 1318 import java.awt.Rectangle;
32 iliev 1286
33     import java.awt.event.ActionEvent;
34     import java.awt.event.ActionListener;
35 iliev 1318 import java.awt.event.HierarchyEvent;
36     import java.awt.event.HierarchyListener;
37 iliev 1286 import java.awt.event.MouseAdapter;
38     import java.awt.event.MouseEvent;
39    
40 iliev 1785 import java.util.Vector;
41    
42     import javax.swing.AbstractButton;
43 iliev 1318 import javax.swing.BorderFactory;
44 iliev 1286 import javax.swing.Box;
45     import javax.swing.BoxLayout;
46     import javax.swing.JPanel;
47 iliev 1318 import javax.swing.JScrollPane;
48 iliev 1785 import javax.swing.JToggleButton;
49 iliev 1286
50     import org.jsampler.CC;
51 iliev 1785 import org.jsampler.view.JSChannelsPane;
52     import org.jsampler.view.fantasia.basic.*;
53 iliev 2288 import org.jsampler.view.swing.SHF;
54     import org.jsampler.view.swing.SwingChannelsPane;
55 iliev 1286
56     import static org.jsampler.view.fantasia.FantasiaI18n.i18n;
57    
58    
59     /**
60     *
61     * @author Grigor Iliev
62     */
63 iliev 1778 public class MainPane extends FantasiaPanel {
64 iliev 1785 private final ChannelsBar channelsBar;
65     private final ButtonsPanel buttonsPanel;
66     private final Vector<ChannelsPanel> channelsPanes = new Vector<ChannelsPanel>();
67 iliev 1286
68 iliev 1785 final JScrollPane scrollPane = new JScrollPane();
69 iliev 1318
70 iliev 1286 /** Creates a new instance of <code>MainPane</code> */
71     public MainPane() {
72 iliev 1778 setOpaque(false);
73 iliev 1872
74     int count = CC.preferences().getIntProperty("channelLanes.count");
75     for(int i = 0; i < count; i++) {
76 iliev 1818 String s = i18n.getButtonLabel("MainPane.ButtonsPanel.tt", i + 1);
77     channelsPanes.add(new ChannelsPanel(s));
78 iliev 1785 }
79    
80 iliev 1872 buttonsPanel = new ButtonsPanel(count);
81     if(count == 1) buttonsPanel.setVisible(false);
82 iliev 1785 channelsBar = new ChannelsBar(buttonsPanel);
83 iliev 1286 GridBagLayout gridbag = new GridBagLayout();
84     GridBagConstraints c = new GridBagConstraints();
85    
86     setLayout(gridbag);
87    
88 iliev 1778 JPanel p = new FantasiaPanel();
89     p.setOpaque(false);
90 iliev 1318 p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
91     p.add(channelsBar);
92     p.add(Box.createGlue());
93    
94 iliev 1286 c.gridx = 0;
95     c.gridy = 0;
96     c.weightx = 1.0;
97 iliev 1318 c.insets = new Insets(0, 0, 0, 0);
98 iliev 1286 c.fill = GridBagConstraints.HORIZONTAL;
99 iliev 1318 gridbag.setConstraints(p, c);
100     add(p);
101 iliev 1286
102 iliev 1318 JScrollPane sp = scrollPane;
103     sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
104     //sp.putClientProperty(SCROLL_PANE_BUTTONS_POLICY, ScrollPaneButtonPolicyKind.NONE);
105     sp.setBorder(BorderFactory.createEmptyBorder());
106     sp.setOpaque(false);
107     javax.swing.JViewport wp = sp.getViewport();
108 iliev 1323 wp.setMinimumSize(new Dimension(400, wp.getMinimumSize().height));
109 iliev 1318 wp.setOpaque(false);
110     sp.setMaximumSize(new Dimension(sp.getMaximumSize().width, Short.MAX_VALUE));
111     sp.getVerticalScrollBar().setBorder(BorderFactory.createEmptyBorder(7, 4, 0, 1));
112     //sp.getVerticalScrollBar().setUnitIncrement(12);
113     sp.setPreferredSize(new Dimension(420, sp.getPreferredSize().height));
114    
115     sp.getVerticalScrollBar().addHierarchyListener(new HierarchyListener() {
116     public void
117     hierarchyChanged(HierarchyEvent e) {
118     if((e.getChangeFlags() & e.SHOWING_CHANGED) != e.SHOWING_CHANGED) {
119     return;
120     }
121    
122     onScrollBarVisibilityChanged();
123     }
124     });
125    
126 iliev 1286 c.gridx = 0;
127     c.gridy = 2;
128     c.weightx = 1.0;
129     c.weighty = 1.0;
130     c.insets = new Insets(0, 0, 0, 0);
131     c.fill = GridBagConstraints.BOTH;
132 iliev 1318 gridbag.setConstraints(sp, c);
133     add(sp);
134 iliev 1286
135 iliev 1323 setMaximumSize(new Dimension(420, Short.MAX_VALUE));
136 iliev 1286 }
137 iliev 1872
138 iliev 2288 public ChannelsPane
139 iliev 1872 addChannelsPane() {
140     String s = i18n.getButtonLabel("MainPane.ButtonsPanel.tt", channelsPanes.size() + 1);
141     ChannelsPanel cp = new ChannelsPanel(s);
142     channelsPanes.add(cp);
143     return cp.getChannelsPane();
144     }
145 iliev 1286
146 iliev 1318 private void
147     onScrollBarVisibilityChanged() {
148 iliev 1323 int w = 420;
149 iliev 1318 int h = scrollPane.getPreferredSize().height;
150     int scrollbarWidth = scrollPane.getVerticalScrollBar().getPreferredSize().width;
151    
152 iliev 1323 if(scrollPane.getVerticalScrollBar().isVisible()) w += scrollbarWidth;
153    
154     scrollPane.setMinimumSize(new Dimension(w, scrollPane.getPreferredSize().height));
155     scrollPane.setPreferredSize(new Dimension(w, h));
156     scrollPane.setMaximumSize(new Dimension(w, Short.MAX_VALUE));
157     setMaximumSize(new Dimension(w, Short.MAX_VALUE));
158    
159 iliev 2288 if(SHF.getMainFrame() != null && !SHF.getMainFrame().isResizable()) {
160 iliev 1323 // this means that there are no visible side panes
161    
162 iliev 2288 w = SHF.getMainFrame().getPreferredSize().width;
163     SHF.getMainFrame().setSize(w, SHF.getMainFrame().getHeight());
164 iliev 1318 }
165    
166     revalidate();
167     }
168    
169     public void
170     scrollToBottom() {
171     int h = scrollPane.getViewport().getView().getHeight();
172     scrollPane.getViewport().scrollRectToVisible(new Rectangle(0, h - 2, 1, 1));
173     }
174    
175 iliev 2288 public ChannelsPane
176 iliev 1785 getChannelsPane(int index) { return channelsPanes.get(index).getChannelsPane(); }
177 iliev 1286
178 iliev 1785 public int
179     getChannelsPaneCount() { return channelsPanes.size(); }
180 iliev 1872
181     public void
182     removeChannelsPane(int index) { channelsPanes.remove(index); }
183 iliev 1785
184     public void
185     setSelectedChannelsPane(JSChannelsPane pane) {
186     ChannelsPanel chnPanel = null;
187    
188     for(int i = 0; i < getChannelsPaneCount(); i++) {
189     if(channelsPanes.get(i).getChannelsPane() == pane) {
190     chnPanel = channelsPanes.get(i);
191    
192     if(!buttonsPanel.buttons.get(i).isSelected()) {
193     buttonsPanel.buttons.get(i).setSelected(true);
194     }
195    
196     break;
197     }
198     }
199    
200     if(chnPanel == null) {
201     CC.getLogger().warning("Unknown channels pane");
202     return;
203     }
204    
205     scrollPane.getViewport().setView(chnPanel);
206     }
207    
208 iliev 2288 public ChannelsPane
209 iliev 1785 getSelectedChannelsPane() {
210     for(int i = 0; i < getChannelsPaneCount(); i++) {
211     if(buttonsPanel.buttons.get(i).isSelected()) {
212     return channelsPanes.get(i).getChannelsPane();
213     }
214     }
215    
216     return null;
217     }
218 iliev 1872
219     public ButtonsPanel
220     getButtonsPanel() { return buttonsPanel; }
221 iliev 1785
222     @Override
223 iliev 1286 protected void
224     paintComponent(Graphics g) {
225     super.paintComponent(g);
226     int h = Res.gfxChannelsBg.getIconHeight();
227     if(h < 1) return;
228     int i = 0;
229     while(i < getHeight()) {
230     Res.gfxChannelsBg.paintIcon(this, g, 0, i);
231     i += h;
232     }
233     }
234    
235 iliev 1776 private final EventHandler eventHandler = new EventHandler();
236    
237     private EventHandler
238     getHandler() { return eventHandler; }
239    
240     private class EventHandler extends MouseAdapter {
241 iliev 1785 @Override
242 iliev 1776 public void
243 iliev 1818 mousePressed(MouseEvent e) {
244 iliev 1776 // TAG: channel selection system
245 iliev 1785 CC.getMainFrame().getSelectedChannelsPane().setSelectedChannel(null);
246 iliev 1776 ///////
247     }
248     }
249    
250 iliev 1785 private class ChannelsPanel extends FantasiaPanel {
251 iliev 2288 private final ChannelsPane channelsPane;
252 iliev 1818 ChannelsPanel(String title) {
253 iliev 1785 ActionListener l = new ActionListener() {
254     public void
255     actionPerformed(ActionEvent e) { scrollToBottom(); }
256     };
257    
258 iliev 1818 channelsPane = new ChannelsPane(title, l);
259 iliev 1785
260     setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
261     channelsPane.setAlignmentX(LEFT_ALIGNMENT);
262     add(channelsPane);
263     JPanel p2 = new NewChannelPane();
264     p2.setAlignmentX(LEFT_ALIGNMENT);
265     add(p2);
266     add(Box.createGlue());
267     setOpaque(false);
268     setBorder(BorderFactory.createEmptyBorder(7, 0, 0, 0));
269     setMinimumSize(new Dimension(420, getMinimumSize().height));
270     setAlignmentX(LEFT_ALIGNMENT);
271    
272     addMouseListener(getHandler());
273     }
274    
275 iliev 2288 public ChannelsPane
276 iliev 1785 getChannelsPane() { return channelsPane; }
277     }
278    
279 iliev 1872 public class ButtonsPanel extends FantasiaToggleButtonsPanel implements ActionListener {
280     public
281     ButtonsPanel(int count) {
282     super(count, true);
283     setButtonNumber(count);
284     }
285    
286     @Override
287     public void
288     setButtonNumber(int number) {
289     if(number > MainFrame.MAX_CHANNEL_LANE_NUMBER) return;
290     int j = number == 7 ? 98 : 96;
291     int k = j / number;
292    
293     for(int i = 0; i < buttons.size(); i++) {
294     buttons.get(i).removeActionListener(this);
295     }
296    
297     super.setButtonNumber(number);
298    
299     for(int i = 0; i < number; i++) {
300 iliev 1785 JToggleButton btn = buttons.get(i);
301     btn.setText(String.valueOf(i + 1));
302 iliev 1872 btn.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
303 iliev 1785 btn.addActionListener(this);
304 iliev 1872 btn.setMinimumSize(new Dimension(k, btn.getPreferredSize().height));
305     btn.setPreferredSize(btn.getMinimumSize());
306     btn.setMaximumSize(btn.getMinimumSize());
307 iliev 1785 String s = "MainPane.ButtonsPanel.tt";
308     btn.setToolTipText(i18n.getButtonLabel(s, i + 1));
309     }
310    
311     setMaximumSize(getPreferredSize());
312     }
313    
314     @Override
315     public void
316     actionPerformed(ActionEvent e) {
317     AbstractButton btn = (AbstractButton)e.getSource();
318     if(!btn.isSelected()) return;
319     int idx = buttons.indexOf(btn);
320     if(idx == -1) return;
321    
322     ChannelsPanel chnPanel = channelsPanes.get(idx);
323     CC.getMainFrame().setSelectedChannelsPane(chnPanel.getChannelsPane());
324     }
325     }
326    
327 iliev 1286 class NewChannelPane extends PixmapPane implements ActionListener {
328     private final PixmapButton btnNew = new PixmapButton(Res.gfxPowerOff);
329    
330     NewChannelPane() {
331     super(Res.gfxCreateChannel);
332     setPixmapInsets(new Insets(3, 3, 3, 3));
333    
334     setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
335 iliev 1732 add(Box.createRigidArea(new Dimension(3, 0)));
336     add(btnNew);
337 iliev 1286 add(Box.createRigidArea(new Dimension(4, 0)));
338    
339     add(createVSeparator());
340    
341     add(Box.createRigidArea(new Dimension(275, 0)));
342    
343     add(createVSeparator());
344    
345     add(Box.createRigidArea(new Dimension(29, 0)));
346    
347     add(createVSeparator());
348    
349     add(Box.createRigidArea(new Dimension(40, 0)));
350    
351     add(createVSeparator());
352    
353     setPreferredSize(new Dimension(420, 29));
354     setMinimumSize(getPreferredSize());
355     setMaximumSize(getPreferredSize());
356    
357     btnNew.setPressedIcon(Res.gfxPowerOn);
358     btnNew.addActionListener(this);
359    
360     addMouseListener(new MouseAdapter() {
361     public void
362     mouseClicked(MouseEvent e) {
363     if(e.getButton() != e.BUTTON1) {
364     return;
365     }
366    
367     CC.getSamplerModel().addBackendChannel();
368     }
369     });
370    
371     setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
372    
373     String s = i18n.getLabel("MainPane.NewChannelPane.newChannel");
374     btnNew.setToolTipText(s);
375     setToolTipText(s);
376     }
377    
378 iliev 1785 @Override
379 iliev 1286 public void
380     actionPerformed(ActionEvent e) {
381     CC.getSamplerModel().addBackendChannel();
382     }
383    
384     private JPanel
385     createVSeparator() {
386     PixmapPane p = new PixmapPane(Res.gfxVLine);
387     p.setOpaque(false);
388     p.setPreferredSize(new Dimension(2, 29));
389     p.setMinimumSize(p.getPreferredSize());
390     p.setMaximumSize(p.getPreferredSize());
391     return p;
392     }
393     }
394     }

  ViewVC Help
Powered by ViewVC