1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package ipodaccesstest;
  26 
  27 import javafx.application.Application;
  28 
  29 import javafx.scene.Group;
  30 import javafx.scene.Scene;
  31 import javafx.scene.paint.Color;
  32 import javafx.stage.Stage;
  33 import javafx.scene.media.MediaException;
  34 
  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.Calendar;
  38 
  39 import java.text.SimpleDateFormat;
  40 import javafx.scene.control.*;
  41 
  42 import com.sun.javafx.ext.device.ios.ipod.MediaQuery;
  43 import com.sun.javafx.ext.device.ios.ipod.MediaItem;
  44 import com.sun.javafx.ext.device.ios.ipod.MediaFilter;
  45 
  46 
  47 public class IPodAccessTest extends Application {
  48     private Scene scene;
  49     private TextArea ta;
  50 
  51     @Override
  52     public void start(final Stage stage) {
  53         stage.setTitle("iPodAccessTest");
  54 
  55         scene = new Scene(new Group());
  56         stage.setHeight(1004);
  57         stage.setWidth(768);
  58         scene.setFill(Color.SKYBLUE);
  59 
  60         ta = new TextArea("");
  61         ScrollPane sp = new ScrollPane();
  62         sp.setContent(ta);
  63         ta.setMinSize(768, 1004);
  64         ta.setEditable(false);
  65 
  66         ((Group)scene.getRoot()).getChildren().addAll(sp);
  67 
  68         stage.setScene(scene);
  69         stage.show();
  70 
  71         try {
  72             testIPodAccess();
  73             testIPodAccessWithGrouping();
  74         }
  75         catch (MediaException e) {
  76             addText("Media exception caught:" + e);
  77             e.printStackTrace();
  78         }
  79     }
  80 
  81     void addText(final String msg) {
  82         ta.textProperty().setValue(ta.textProperty().getValue() + msg + "\n");
  83     }
  84 
  85     private void printMediaItemProperties(final MediaItem item) {
  86         addText("Item: " + item);
  87         addText("      media type :        " + item.getMediaType());
  88         addText("      title:              " + item.getTitle());
  89         addText("      album title:        " + item.getAlbumTitle());
  90         addText("      artist:             " + item.getArtist());
  91         addText("      album artist:       " + item.getAlbumArtist());
  92         addText("      genre:              " + item.getGenre());
  93         addText("      composer:           " + item.getComposer());
  94         addText("      playback duration:  " + item.getPlaybackDuration());
  95         addText("      album track number: " + item.getAlbumTrackNumber());
  96         addText("      album track count:  " + item.getAlbumTrackCount());
  97         addText("      disc number:        " + item.getDiscNumber());
  98         addText("      disc count:         " + item.getDiscCount());
  99         // missing artwork
 100         addText("      lyrics:             " + item.getLyrics());
 101         addText("      is compilation ?    " + item.isCompilation());
 102 
 103         final Calendar date = item.getReleaseDate();
 104         if (date != null) {
 105             final SimpleDateFormat df = new SimpleDateFormat();
 106             df.applyPattern("dd/MM/yyyy");
 107             addText("      release date:       " + df.format(date.getTime()));
 108         } else {
 109             addText("      release date:       N/A");
 110         }
 111         addText("      beats per minute:   " + item.getBeatsPerMinute());
 112         addText("      comments:           " + item.getComments());
 113         addText("      url:                " + item.getURL());
 114     }
 115 
 116     private void testIPodAccess() {
 117         addText("Testing iPod library access extension API");
 118         final MediaQuery mediaQuery = new MediaQuery();
 119 
 120         addText("Retrieving media items");
 121         final List<MediaItem> items = mediaQuery.getItems();
 122 
 123         addText("Enumerating " + items.size() + " media items...");
 124         for (final MediaItem item : items) {
 125             printMediaItemProperties(item);
 126         }
 127     }
 128 
 129     private void testIPodAccessWithGrouping() {
 130         addText("Testing iPod library access extension");
 131         final MediaQuery mediaQuery = new MediaQuery();
 132 
 133         addText("Adding a filter (Artist must be \"RingtoneFeeder.com\")");
 134         final MediaFilter filter = new MediaFilter(MediaFilter.MediaFilterType.Artist, "RingtoneFeeder.com");
 135         mediaQuery.addFilter(filter);
 136 
 137         addText("Setting grouping type");
 138         mediaQuery.setGroupingType(MediaQuery.MediaGroupingType.AlbumArtist);
 139 
 140         addText("Retrieving media items collections");
 141         final List<List<MediaItem>> collections = mediaQuery.getCollections();
 142 
 143         addText("There are " + collections.size() + " collections");
 144 
 145         int i=0;
 146         for (final List<MediaItem> collection : collections) {
 147             addText("Collection #" + i++);
 148             final Iterator<MediaItem> iterator = collection.iterator();
 149             while (iterator.hasNext()) {
 150                 final MediaItem item = iterator.next();
 151                 printMediaItemProperties(item);
 152             }
 153         }
 154     }
 155 
 156     public static void main(String[] args) {
 157         Application.launch(args);
 158     }
 159 }