demos\covergirls\covergirls.js
  1 /** compound icon to use for play album rollover effect*/
  2 var compoundIcon=sage.createCompoundIcon(null,null);
  3 
  4 
  5 /**
  6  *Toggles the maximize state
  7  */
  8 function toggleMaximize() {
  9   if(window.nativeEvent.clickCount>1) {
 10     if(window.isMaximized()) {
 11       window.normal()
 12     }
 13     else {
 14       window.maximize()
 15     }
 16   }
 17 }
 18 /**
 19  *Handles mouse leaving of the window controls image
 20  */
 21 function winControlMouseOut() {
 22   if(sage.isMac()) {
 23     widget.icon=window.getResourceIcon('icon_window_controls_mac')
 24   }
 25   else {
 26     if(window.isMaximized()) {
 27       widget.icon=window.getResourceIcon('icon_window_controls_max_norm')
 28     }
 29     else {
 30       widget.icon=window.getResourceIcon('icon_window_controls_norm')
 31     }
 32   }
 33 }
 34 
 35 /**
 36  *Handles rollover of the window controls image
 37  */
 38 function winControlMouseMoved() {
 39   if(sage.isMac()) {
 40     widget.icon=window.getResourceIcon('icon_window_controls_mac_hover')
 41   }
 42   else {
 43     var x=window.nativeEvent.x
 44     var s=""
 45     if(window.isMaximized()) {
 46       s="_max"
 47     }
 48     if(x<26) {
 49       widget.icon=window.getResourceIcon('icon_window_controls'+s+'_min')
 50     }
 51     else if(x<51) {
 52       widget.icon=window.getResourceIcon('icon_window_controls'+s+'_max')
 53     }
 54     else {
 55       widget.icon=window.getResourceIcon('icon_window_controls'+s+'_close')
 56     }
 57   }
 58 }
 59 
 60 /**
 61  *Handles a mouse click on the window controls image
 62  */
 63 function winControlMouseClick() {
 64   var x=window.nativeEvent.x
 65   if(sage.isMac()) {
 66     if(x>3 && x<18) {
 67       window.close()
 68     }
 69     else if(x>24 && x<39) {
 70       window.minimize()
 71     }
 72     else if(x>45 && x<60) {
 73       if(window.isMaximized()) {
 74         window.normal()
 75       }
 76       else {
 77         window.maximize()
 78       }
 79     }
 80   }
 81   else {
 82     if(x<26) {
 83       window.minimize()
 84     }
 85     else if(x<51) {
 86       if(window.isMaximized()) {
 87         window.normal()
 88       }
 89       else {
 90         window.maximize()
 91       }
 92     }
 93     else {
 94       window.close()
 95     }
 96   }
 97 }
 98 /**
 99  *Called when the window is created but before it is fully configured
100  */
101 function window_onCreated() {
102   if(!sage.isWindows()) {
103     var cfg=window.nativeEvent.data
104     cfg.spot_setAttribute('opaque','true')
105     if(sage.isMac()) {
106       cfg.windowPainter=null
107     }
108     else {
109       cfg.windowPainter.borders[0].spot_setAttribute('cornerArc','0')
110     }
111     cfg.decorated.value=true
112   }
113 }
114 /**
115  *Called when the window is created but before it is fully configured
116  */
117 function window_onConfigure() {
118   if(!sage.isMac()) {
119     //do this for mac also because the current 1.6 version does no support the unified toolbar properly
120     var lostFocusPainter=sage.createBackgroundPainter(window.getBackgroundColor("#e4e4e4, #d0d0d0"))
121     var focusPainter=sage.createBackgroundPainter(window.getBackgroundColor("#bbbbbb, #979797"))
122     focusPainter.displayed=DISPLAYED_WHEN_WINDOW_FOCUSED
123     lostFocusPainter.displayed=DISPLAYED_WHEN_WINDOW_NOT_FOCUSED
124     var cp=sage.createCompoundPainter(focusPainter,lostFocusPainter)
125     window.getTarget('header').backgroundPainter=cp;
126     window.getTarget('footer').backgroundPainter=cp;
127   }
128 }
129 
130 /**
131  * Called when the header form is created but
132  * before it is configured. If the user is running on a Mac
133  * then this code changes the configuration object to show
134  * the Mac window controls and to use the default menu bar
135  */
136 function header_onCreated() {
137   if(!sage.isWindows()) {
138     var cfg=window.nativeEvent.data
139     var menu=cfg.findWidget("menuBar")
140     var controls=cfg.findWidget("windowControls")
141     //    controls.bounds.x.value=menu.bounds.x.value
142     //    controls.bounds.y.value=menu.bounds.y.value
143     //    controls.horizontalAlign.value="left"
144     //    controls.verticalAlign.value="center"
145     //    controls.icon.value="resource:icon_window_controls_mac"
146     //    controls.columnSpan.value=menu.columnSpan.value
147     cfg.removeWidget(controls)
148     cfg.removeWidget(menu)
149     cfg.removeWidget(cfg.findWidget("tunesLabel"))
150     cfg.findWidget("songInfo").columnSpan.value=1
151     cfg.rows.value="2px,d,4dlu"
152     cfg.columns.value="14dlu,d,d,d,14px,d,32dlu,d:g,d,14dlu,d,4dlu"
153     window.setMenubar(window.createViewer(window,menu))
154   }
155 }
156 
157 /**
158  * Code that removes the play album graphic
159  */
160 function gridViewMouseOut() {
161   var item=widget.removeData("rolloverItem");
162   if(item!=null) {
163     item.icon=widget.removeData("rolloverItemIcon");
164     widget.update()
165   }
166 }
167 
168 /**
169  * Code that shows the play album graphic
170  * when the mouse moves over an album
171  */
172 function gridViewMouseMoved() {
173   var point=window.nativeEvent.point
174   var index=widget.getRowIndexAt(point)
175   if(index==-1) {
176     gridViewMouseOut()
177     return;
178   }
179   var compositer=widget.getItemDescription().getCellRenderer()
180   var r=widget.dataComponent.getCellBounds(index, index)
181   r.height=compositer.iconLabel.height-10;
182   r.width-=10
183   r.x+=5
184   r.y+=10
185   if(!r.contains(point)) {
186     gridViewMouseOut()
187     return;
188   }
189   var item=widget.getData("rolloverItem");
190   var nitem=widget.get(index)
191   if(item!=nitem) {
192     if(item!=null) {
193       item.icon=widget.getData("rolloverItemIcon");
194     }
195     widget.putData("rolloverItem",nitem);
196     widget.putData("rolloverItemIcon",nitem.icon);
197     compoundIcon.firstIcon=nitem.icon
198     nitem.icon=compoundIcon
199     widget.update()
200   }
201 }
202 
203 function gridViewMouseClicked() {
204   var item=widget.getData("rolloverItem");
205   var index=item=widget.selectedIndex
206   if(item!=null && index!=-1) {
207     var compositer=widget.getItemDescription().getCellRenderer()
208     var r=widget.dataComponent.getCellBounds(index, index)
209     var psize=compositer.iconLabel.getPreferredSize()
210     var pr=compoundIcon.secondIcon.getPaintRect(compositer.iconLabel,0,0,psize.width,psize.height)
211     pr.x+=r.x
212     pr.y+=r.y
213     if(pr.contains(window.nativeEvent.point)) {
214       toggleSongsPlaying()
215     }
216   }
217 }
218 function makeResizeCorner() {
219   if(sage.isWindows()) {
220     widget.renderType=RENDERTYPE_LOWER_RIGHT;widget.setComponent(sage.makeResizable(window,true,window.getIcon('lib:images/resize_corner.png')))
221   }
222 }
223 /**
224  * Configures the composite render that the grid view uses
225  * based on the paint information specified in the item description
226  * structure
227  */
228 function gridViewConfigureRenderer() {
229   var compositer=widget.getItemDescription().getCellRenderer()
230   compositer.setBorder(new com.sparseware.sage.border.EmptyBorderEx(5,5,5,5))
231   compositer.makeIconPrimary();
232   compositer.useAlternateIcon=true
233   var pb=widget.itemDescription.headerSelectionPainter
234   compoundIcon.secondIcon=pb.imagePainter
235   compoundIcon.secondIcon.useComponentSizeForIconPaint=true;
236   compositer.iconLabel.setPreferredSize(new java.awt.Dimension(128,128))
237   pb.imagePainter=null
238   widget.dataComponent.cellRenderer.selectionPaint=pb
239   widget.dataComponent.cellRenderer.nonSelectionPaint=widget.itemDescription.headerPainter
240 }
241 
242 /**
243  * Called when the slider in the grid view changes
244  */
245 function gridViewSliderChange() {
246   var compositer=gridViewList.getItemDescription().getCellRenderer()
247   var size=widget.valueAsInt
248   gridViewList.dataComponent.fixedCellWidth=size
249   var psize=compositer.iconLabel.getPreferredSize()
250   if(psize.width!=size) {
251     psize.width=size
252     psize.height=size
253     compositer.iconLabel.setPreferredSize(psize)
254     gridViewList.refreshItems()
255   }
256 }
257 
258 /**
259  * Creates a scroll panel to add to the cover flow panel
260  * Normally the cover flow viewer just has a scrollbar.
261  * This code retrieves the scrollbar and places it on a panel
262  * along with the full screen toggle button
263  */
264 function createCoverFlowScrollPanel(coverflow) {
265   var sb=coverflow.getScrollBar()
266   var panel=sage.createHorizontalBox()
267   panel.add(panel.createHorizontalStrut(60));
268   panel.add(sb)
269   panel.add(panel.createHorizontalStrut(40));
270   panel.opaque=false
271   var b=sage.createButton(window).dataComponent
272   b.opaque=false
273   b.contentAreaFilled=false
274   b.icon=window.getIcon("lib:images/fullscreen.png")
275   b.pressedIcon=window.getIcon("lib:images/fullscreen_pressed.png")
276   panel.add(b)
277   panel.add(b)
278   // adds the scroll panel, the first parameter is the component and the second is the adjustable interface
279   coverflow.setScrollBar(panel,sb)
280 }
281 
282 /**
283  * Toggles the playing of a song both by clicking on the pause/play button
284  * and by double clicking on a song.
285  */
286 function toggleSongsPlaying() {
287   var pause=false
288   var orow
289   var row
290   var songs=window.getViewer("songs") //use the get viewer method in order to make sure that the songs table is on screen
291   if(songs!=null) {
292     row=songs.selectedIndex
293     if(row!=-1) {
294       songs.getItemAt(row,0).icon=window.getResourceIcon("icon_playing")
295       orow=songs.linkedData
296       if(orow!=null) {
297         songs.getItemAt(orow,0).icon=null
298         songs.linkedData=null
299         if(row!=orow) {
300           pause=true
301         }
302       }
303       else {
304         songs.linkedData=row
305         pause=true
306       }
307       songs.update()
308     }
309   }
310   else if(window.getViewer("gridViewList")!=null){
311     var list=window.getViewer("gridViewList")
312     row=list.selectedIndex
313     if(row!=-1) {
314       list.getItem(row).alternateIcon=window.getResourceIcon("icon_playing")
315       orow=list.linkedData
316       if(orow!=null) {
317         list.getItem(orow).alternateIcon=null
318         list.linkedData=null
319         if(row!=orow) {
320           pause=true
321         }
322       }
323       else {
324         list.linkedData=row
325         pause=true
326       }
327       list.update()
328     }
329   }
330   var button=headerForm.getWidget("play").dataComponent
331   if(pause) {
332     button.icon=window.getResourceIcon("icon_pause")
333     button.pressedIcon=window.getResourceIcon("icon_pause_pressed")
334   }
335   else {
336     button.icon=window.getResourceIcon("icon_play")
337     button.pressedIcon=window.getResourceIcon("icon_play_pressed")
338   }
339 }
340 
341 /**
342  * Toggles the checkbok on the songs table
343  */
344 function toggleSongsCheckbox(e) {
345   var t=e.source
346   var p=e.point
347   var row=t.rowAtPoint(p)
348   var index=t.columnAtPoint(p)
349   var col=index
350   if(index!=-1) {
351     index=t.convertColumnIndexToModel(index)
352   }
353   if(index==1 && row>-1) {
354     var r=t.getCellRect(row,col,false)
355     r.x+=7
356     r.y+=6
357     r.width=10
358     r.height=16
359     if(r.contains(p)) {
360       var item=widget.getItemAt(row,index)
361       if(item.icon==null) {
362         item.icon=window.getResourceIcon("icon_checkbox")
363       }
364       else {
365         item.icon=null //will use the default
366       }
367       widget.repaint()
368     }
369   }
370 }
371 
372 function mainWindow_onBlur() {
373 }
374 function mainWindow_onFocus() {
375 }
376