Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/virtuosotechnologies/asaph/standardgui/RenderPane.java


1   /*
2   ================================================================================
3   
4     FILE:  RenderPane.java
5     
6     PROJECT:
7     
8       Asaph
9     
10    CONTENTS:
11    
12      A pane that includes a scrolling rendering.
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.Graphics;
46  import java.awt.Graphics2D;
47  import java.awt.Color;
48  import java.awt.Dimension;
49  import java.awt.BorderLayout;
50  import java.awt.event.ComponentAdapter;
51  import java.awt.event.ComponentEvent;
52  import java.awt.event.HierarchyListener;
53  import java.awt.event.HierarchyEvent;
54  import java.io.File;
55  import java.io.FileWriter;
56  import java.io.StringWriter;
57  import java.io.IOException;
58  import javax.swing.JComponent;
59  import javax.swing.JPanel;
60  import javax.swing.JScrollPane;
61  import javax.swing.JFileChooser;
62  
63  import com.virtuosotechnologies.lib.util.ExceptionUtils;
64  import com.virtuosotechnologies.lib.swing.DetailedMessageDialog;
65  
66  import com.virtuosotechnologies.asaph.model.Song;
67  import com.virtuosotechnologies.asaph.model.Variation;
68  import com.virtuosotechnologies.asaph.model.ChordSet;
69  import com.virtuosotechnologies.asaph.model.notation.Interval;
70  import com.virtuosotechnologies.asaph.modelutils.SongUtils;
71  import virtuoso.asaph.util.render.PlainTextBodyRender;
72  
73  
74  /**
75   * A pane that includes a scrolling rendering.
76   */
77  /*package*/ class RenderPane
78  {
79    private static final String STR_dialog_ErrorTitle =
80      ResourceAccess.Strings.buildString("dialog_ErrorTitle");
81    private static final String STR_message_ExportError =
82      ResourceAccess.Strings.buildString("message_ExportError");
83    private static final String STR_dialog_ErrorsHeader =
84      ResourceAccess.Strings.buildString("dialog_ErrorsHeader");
85    private static final String STR_dialog_FirstExceptionTemplate =
86      ResourceAccess.Strings.buildString("dialog_FirstExceptionTemplate");
87    private static final String STR_dialog_NextExceptionTemplate =
88      ResourceAccess.Strings.buildString("dialog_NextExceptionTemplate");
89    
90    
91    private SongUtils songUtils_;
92    
93    private Song song_;
94    private Variation variation_;
95    private ChordSet chordSet_;
96    private Interval curTranspose_;
97    private String curKeyInfo_;
98    private RenderSettings settings_;
99    
100   private RenderHelper helper_;
101   private RenderComponent renderComponent_;
102   
103   private JComponent mainPane_;
104   
105   
106   /**
107    * Constructor
108    */
109   /*package*/ RenderPane(
110     Song song,
111     RenderSettings settings,
112     SongUtils songUtils)
113   {
114     songUtils_ = songUtils;
115     song_ = song;
116     variation_ = null;
117     chordSet_ = null;
118     curTranspose_ = null;
119     settings_ = settings;
120     curKeyInfo_ = "";
121     
122     renderComponent_ = new RenderComponent();
123     renderComponent_.addComponentListener(
124       new ComponentAdapter()
125       {
126         public void componentResized(
127           ComponentEvent ev)
128         {
129           updateDisplay();
130         }
131       });
132     renderComponent_.addHierarchyListener(
133       new HierarchyListener()
134       {
135         public void hierarchyChanged(
136           HierarchyEvent ev)
137         {
138           if ((ev.getChangeFlags() & HierarchyEvent.DISPLAYABILITY_CHANGED) != 0 &&
139             ev.getComponent().isDisplayable())
140           {
141             updateDisplay();
142           }
143         }
144       });
145     
146     JPanel bodyPanel = new JPanel(new BorderLayout());
147     bodyPanel.add(renderComponent_, BorderLayout.NORTH);
148     bodyPanel.add(new FillerComponent(100, 0), BorderLayout.CENTER);
149     
150     JScrollPane scrollPane = new JScrollPane(bodyPanel,
151       JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
152     mainPane_ = scrollPane;
153   }
154   
155   
156   /*package*/ void setChords(
157     ChordSet chordSet,
158     Interval transpose,
159     String keyInfo)
160   {
161     chordSet_ = chordSet;
162     curTranspose_ = transpose;
163     curKeyInfo_ = keyInfo;
164     updateDisplay();
165   }
166   
167   
168   /*package*/ void setVariation(
169     Variation variation)
170   {
171     variation_ = variation;
172     updateDisplay();
173   }
174   
175   
176   /*package*/ JComponent getJComponent()
177   {
178     return mainPane_;
179   }
180   
181   
182   /**
183    * Perform print
184    */
185   /*package*/ RenderHelper getCurRenderHelper()
186   {
187     return helper_;
188   }
189   
190   
191   /**
192    * Perform export text
193    */
194   /*package*/ void doExportText()
195   {
196     JFileChooser chooser = new JFileChooser();
197     chooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
198     int returnVal = chooser.showSaveDialog(mainPane_);
199     if (returnVal == JFileChooser.APPROVE_OPTION)
200     {
201       FileWriter fileWriter = null;
202       try
203       {
204         fileWriter = new FileWriter(chooser.getSelectedFile());
205         
206         String str = songUtils_.getFieldValueAsString(song_, Song.MAINTITLE_FIELD);
207         if (str.length() > 0)
208         {
209           fileWriter.write(str);
210           fileWriter.write("\n\n");
211         }
212         str = songUtils_.getFieldValueAsString(song_, Song.AUTHOR_FIELD);
213         if (str.length() > 0)
214         {
215           fileWriter.write(str);
216           fileWriter.write("\n");
217         }
218         str = songUtils_.getFieldValueAsString(song_, Song.COPYRIGHT_FIELD);
219         if (str.length() > 0)
220         {
221           fileWriter.write(str);
222           fileWriter.write("\n");
223         }
224         fileWriter.write("\n\n");
225         
226         PlainTextBodyRender render = new PlainTextBodyRender();
227         render.setLineSpacing(0);
228         render.setBlockSpacing(2);
229         StringWriter writer = new StringWriter();
230         render.writeBodyText(writer, song_, chordSet_, curTranspose_);
231         fileWriter.write(writer.toString());
232         fileWriter.write("\n");
233       }
234       catch (IOException ex)
235       {
236         DetailedMessageDialog dialog = DetailedMessageDialog.create(
237           mainPane_, STR_dialog_ErrorTitle, STR_message_ExportError,
238           STR_dialog_ErrorsHeader, ExceptionUtils.getDescriptionFor(ex,
239             STR_dialog_FirstExceptionTemplate, STR_dialog_NextExceptionTemplate),
240           null);
241         dialog.show();
242       }
243       finally
244       {
245         if (fileWriter != null)
246         {
247           try
248           {
249             fileWriter.close();
250           }
251           catch (IOException ex)
252           {
253           }
254         }
255       }
256     }
257   }
258   
259   
260   /*package*/ void updateDisplay()
261   {
262     if (renderComponent_.isDisplayable())
263     {
264       float curZoom = settings_.getZoom();
265       helper_ = new RenderHelper((Graphics2D)renderComponent_.getGraphics(),
266         songUtils_, song_, variation_, chordSet_, curTranspose_, curKeyInfo_,
267         settings_, ((float)renderComponent_.getWidth())/curZoom);
268       renderComponent_.setPreferredSize(new Dimension((int)(helper_.getMinWidth()*curZoom),
269         (int)(helper_.getHeight()*curZoom)));
270       renderComponent_.invalidate();
271       mainPane_.revalidate();
272       mainPane_.repaint();
273     }
274   }
275   
276   
277   private class RenderComponent
278   extends JComponent
279   {
280     public void paintComponent(
281       Graphics g)
282     {
283       Graphics2D g2d = (Graphics2D)g;
284       
285       RenderHelper.mungeGraphics2D(g2d);
286       
287       g2d.setColor(Color.white);
288       g2d.fillRect(0, 0, getWidth(), getHeight());
289       g2d.setColor(Color.black);
290       if (helper_ != null)
291       {
292         float curZoom = settings_.getZoom();
293         g2d.scale(curZoom, curZoom);
294         helper_.paint(g2d, 0, 0);
295       }
296     }
297   }
298   
299   
300   private class FillerComponent
301   extends JComponent
302   {
303     private FillerComponent(
304       int width,
305       int height)
306     {
307       setPreferredSize(new Dimension(width, height));
308     }
309     
310     public void paintComponent(
311       Graphics g)
312     {
313       g.setColor(Color.white);
314       g.fillRect(0, 0, getWidth(), getHeight());
315     }
316   }
317 }