Ignore:
Timestamp:
Jan 21, 2011, 7:18:42 PM (13 years ago)
Author:
mlg
Message:

Changes ImagesManagement access from static to object.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/jiwigo/trunk/src/main/java/fr/mael/jiwigo/ui/mainframe/ThumbnailCategoryPanel.java

    r7223 r8829  
    66import java.io.File;
    77import java.io.IOException;
     8
    89import javax.swing.JOptionPane;
    910import javax.swing.JPanel;
     11
    1012import fr.mael.jiwigo.om.Category;
    1113import fr.mael.jiwigo.om.Image;
     
    1618import fr.mael.jiwigo.transverse.util.filedrop.FileDrop;
    1719import fr.mael.jiwigo.ui.layout.VerticalLayout;
    18 
    1920
    2021/**
     
    4950public class ThumbnailCategoryPanel extends JPanel implements IThumbnailPanel {
    5051
    51         /**
    52          * Logger
    53          */
    54         public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(ThumbnailCategoryPanel.class);
    55         /**
    56          * id of the category
    57          */
    58         private Integer categoryId;
    59 
    60         private Category category;
    61 
    62         /**
    63          * thumbnails per line
    64          */
    65         private int thumbnailPerLine = 7;
    66 
    67         /**
    68          * Saved dimension of the panel, used to define the number
    69          * of thumbnails on a line
    70          */
    71         private Dimension savedDimension = new Dimension();
    72 
    73 
    74         public ThumbnailCategoryPanel(Category category) {
    75                 this(category.getIdentifiant());
    76                 this.category = category;
    77         }
    78 
    79 
    80         /**
    81          * Constructor
    82          * @param categoryId id of the category
    83          */
    84         public ThumbnailCategoryPanel(Integer categoryId) {
    85                 this.categoryId = categoryId;
    86                 this.setLayout(new VerticalLayout());
    87                 if (categoryId != null) {
    88                         rafraichir(categoryId, false);
    89                 }
    90                 //gestion du drag'n drop
    91                 new FileDrop(System.out, this, new FileDrop.Listener() {
    92 
    93                         public void filesDropped(final java.io.File[] files) {
    94                                 if (!ImagesManagement.rememberPrivacyLevel) {
    95                                         new DialogPrivacyLevel();
    96                                 }
    97                                 if (ImagesManagement.sendingFiles) {
    98                                         JOptionPane.showMessageDialog(null, Messages.getMessage("alreadySendingError"), Messages.getMessage("error"),
    99                                                         JOptionPane.ERROR_MESSAGE);
    100                                 } else {
    101                                         new Thread(new ThreadEnvoiPhoto(files)).start();
    102                                 }
    103                         }
    104                 });
    105 
    106         }
    107 
     52    /**
     53     * Logger
     54     */
     55    public static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory
     56            .getLog(ThumbnailCategoryPanel.class);
     57    /**
     58     * id of the category
     59     */
     60    private Integer categoryId;
     61
     62    private Category category;
     63
     64    /**
     65     * thumbnails per line
     66     */
     67    private int thumbnailPerLine = 7;
     68
     69    /**
     70     * Saved dimension of the panel, used to define the number
     71     * of thumbnails on a line
     72     */
     73    private Dimension savedDimension = new Dimension();
     74
     75    private ImagesManagement imagesManagement;
     76
     77    private ThumbnailCategoryPanel() {
     78        // TODO Auto-generated constructor stub
     79        imagesManagement = ImagesManagement.getInstance();
     80    }
     81
     82    public ThumbnailCategoryPanel(Category category) {
     83        this(category.getIdentifiant());
     84        this.category = category;
     85    }
     86
     87    /**
     88     * Constructor
     89     * @param categoryId id of the category
     90     */
     91    public ThumbnailCategoryPanel(Integer categoryId) {
     92        this();
     93        this.categoryId = categoryId;
     94        this.setLayout(new VerticalLayout());
     95        if (categoryId != null) {
     96            rafraichir(categoryId, false);
     97        }
     98        //gestion du drag'n drop
     99        new FileDrop(System.out, this, new FileDrop.Listener() {
     100
     101            public void filesDropped(final java.io.File[] files) {
     102                if (!imagesManagement.isRememberPrivacyLevel()) {
     103                    new DialogPrivacyLevel();
     104                }
     105                if (imagesManagement.isSendingFiles()) {
     106                    JOptionPane.showMessageDialog(null, Messages.getMessage("alreadySendingError"), Messages
     107                            .getMessage("error"), JOptionPane.ERROR_MESSAGE);
     108                } else {
     109                    new Thread(new ThreadEnvoiPhoto(files)).start();
     110                }
     111            }
     112        });
     113
     114    }
     115
     116    @Override
     117    public void paint(Graphics g) {
     118        super.paint(g);
     119        if (!MainFrame.getInstance().getSize().equals(savedDimension)) {
     120            //                  LOG.debug("paint " + getSize());
     121            savedDimension = MainFrame.getInstance().getSize();
     122            int width = savedDimension.width;
     123            thumbnailPerLine = width / 150;
     124            addThumbnails();
     125        }
     126    }
     127
     128    /**
     129     * refreshes the panel
     130     * @param categoryId the id of the category
     131     */
     132    public void rafraichir(Integer categoryId, boolean rafraichir) {
     133        this.categoryId = categoryId;
     134        try {
     135            MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading"));
     136            imagesManagement.setListImage(ImageService.getInstance().listerParCategory(categoryId, rafraichir));
     137            addThumbnails();
     138            this.repaint();
     139            this.revalidate();
     140            MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk"));
     141        } catch (Exception e) {
     142            LOG.error(Outil.getStackTrace(e));
     143            JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"),
     144                    Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
     145            MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError"));
     146        }
     147    }
     148
     149    /**
     150     * Adds the thumbnails to the panel
     151     */
     152    private void addThumbnails() {
     153        this.removeAll();
     154        int nb = 1;
     155        JPanel panelh = new JPanel(new FlowLayout());
     156        try {
     157            imagesManagement.setListImage(ImageService.getInstance().listerParCategory(categoryId, false));
     158        } catch (IOException e1) {
     159            e1.printStackTrace();
     160        }
     161        for (Image image : imagesManagement.getListImage()) {
     162            try {
     163
     164                if (nb == thumbnailPerLine) {
     165                    this.add(panelh);
     166                    panelh = new JPanel(new FlowLayout());
     167                    nb = 0;
     168                } else {
     169                    ThumbnailPanel panel = new ThumbnailPanel(image);
     170                    panelh.add(panel);
     171                }
     172                nb++;
     173
     174            } catch (Exception e) {
     175
     176            }
     177        }
     178        if (nb != thumbnailPerLine + 1) {
     179            this.add(panelh);
     180        }
     181    }
     182
     183    /**
     184     * @return the categoryId
     185     */
     186    public Integer getCategoryId() {
     187        return categoryId;
     188    }
     189
     190    /**
     191     * @param categoryId the categoryId to set
     192     */
     193    public void setCategoryId(Integer categoryId) {
     194        this.categoryId = categoryId;
     195    }
     196
     197    /**
     198     * @return the category
     199     */
     200    public Category getCategory() {
     201        return category;
     202    }
     203
     204    /**
     205     * @param category the category to set
     206     */
     207    public void setCategory(Category category) {
     208        this.category = category;
     209    }
     210
     211    /**
     212     * @author mael
     213     * Thread that send the photos
     214     */
     215    public class ThreadEnvoiPhoto implements Runnable {
     216
     217        private File[] files;
     218
     219        public ThreadEnvoiPhoto(File[] files) {
     220            this.files = files;
     221        }
    108222
    109223        @Override
    110         public void paint(Graphics g) {
    111                 super.paint(g);
    112                 if (!MainFrame.getInstance().getSize().equals(savedDimension)) {
    113                         //                      LOG.debug("paint " + getSize());
    114                         savedDimension = MainFrame.getInstance().getSize();
    115                         int width = savedDimension.width;
    116                         thumbnailPerLine = width / 150;
    117                         addThumbnails();
    118                 }
    119         }
    120 
    121 
    122         /**
    123          * refreshes the panel
    124          * @param categoryId the id of the category
    125          */
    126         public void rafraichir(Integer categoryId, boolean rafraichir) {
    127                 this.categoryId = categoryId;
     224        public void run() {
     225            imagesManagement.setSendingFiles(true);
     226            for (int i = 0; i < files.length; i++) {
     227                MainFrame.getInstance().setAdditionalMessage(
     228                        "<html><i>" + (i + 1) + "/" + files.length + " : </i></html>");
     229                int nbProgressBar = ((i + 1) * 100) / files.length;
    128230                try {
    129                         MainFrame.getInstance().setMessage(Messages.getMessage("thumbviewer_loading"));
    130                         ImagesManagement.LIST_IMAGE = ImageService.getInstance().listerParCategory(categoryId, rafraichir);
    131                         addThumbnails();
    132                         this.repaint();
    133                         this.revalidate();
    134                         MainFrame.getInstance().setMessage(Messages.getMessage("loadingOk"));
     231
     232                    ImageService.getInstance().creer(files[i].getCanonicalPath(), categoryId);
     233                    MainFrame.getInstance()
     234                            .setMessage(files[i].getName() + " " + Messages.getMessage("sendingSuccess"));
    135235                } catch (Exception e) {
    136                         LOG.error(Outil.getStackTrace(e));
    137                         JOptionPane.showMessageDialog(null, Messages.getMessage("imagesListingError"), Messages.getMessage("error"),
    138                                         JOptionPane.ERROR_MESSAGE);
    139                         MainFrame.getInstance().setMessage(Messages.getMessage("imagesListingError"));
    140                 }
    141         }
    142 
    143 
    144         /**
    145          * Adds the thumbnails to the panel
    146          */
    147         private void addThumbnails() {
    148                 this.removeAll();
    149                 int nb = 1;
    150                 JPanel panelh = new JPanel(new FlowLayout());
    151                 try {
    152                         ImagesManagement.LIST_IMAGE = ImageService.getInstance().listerParCategory(categoryId, false);
    153                 } catch (IOException e1) {
    154                         e1.printStackTrace();
    155                 }
    156                 for (Image image : ImagesManagement.LIST_IMAGE) {
    157                         try {
    158 
    159                                 if (nb == thumbnailPerLine) {
    160                                         this.add(panelh);
    161                                         panelh = new JPanel(new FlowLayout());
    162                                         nb = 0;
    163                                 } else {
    164                                         ThumbnailPanel panel = new ThumbnailPanel(image);
    165                                         panelh.add(panel);
    166                                 }
    167                                 nb++;
    168 
    169                         } catch (Exception e) {
    170 
    171                         }
    172                 }
    173                 if (nb != thumbnailPerLine + 1) {
    174                         this.add(panelh);
    175                 }
    176         }
    177 
    178 
    179         /**
    180          * @return the categoryId
    181          */
    182         public Integer getCategoryId() {
    183                 return categoryId;
    184         }
    185 
    186 
    187         /**
    188          * @param categoryId the categoryId to set
    189          */
    190         public void setCategoryId(Integer categoryId) {
    191                 this.categoryId = categoryId;
    192         }
    193 
    194 
    195         /**
    196          * @return the category
    197          */
    198         public Category getCategory() {
    199                 return category;
    200         }
    201 
    202 
    203         /**
    204          * @param category the category to set
    205          */
    206         public void setCategory(Category category) {
    207                 this.category = category;
    208         }
    209 
    210         /**
    211          * @author mael
    212          * Thread that send the photos
    213          */
    214         public class ThreadEnvoiPhoto implements Runnable {
    215 
    216                 private File[] files;
    217 
    218 
    219                 public ThreadEnvoiPhoto(File[] files) {
    220                         this.files = files;
    221                 }
    222 
    223 
    224                 @Override
    225                 public void run() {
    226                         ImagesManagement.sendingFiles = true;
    227                         for (int i = 0; i < files.length; i++) {
    228                                 MainFrame.getInstance().setAdditionalMessage("<html><i>" + (i + 1) + "/" + files.length + " : </i></html>");
    229                                 int nbProgressBar = ((i + 1) * 100) / files.length;
    230                                 try {
    231 
    232                                         ImageService.getInstance().creer(files[i].getCanonicalPath(), categoryId);
    233                                         MainFrame.getInstance().setMessage(files[i].getName() + " " + Messages.getMessage("sendingSuccess"));
    234                                 } catch (Exception e) {
    235                                         LOG.error(Outil.getStackTrace(e));
    236                                         //displays a dialog if there is an error
    237                                         JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
    238                                                         Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
    239                                         MainFrame.getInstance().setMessage(Messages.getMessage("sendingError") + " " + files[i].getName());
    240                                 } finally {
    241                                         MainFrame.getInstance().getProgressBar().setValue(nbProgressBar);
    242                                 }
    243                         }
    244                         //refresh
    245                         MainFrame.getInstance().setAdditionalMessage("");
    246                         rafraichir(categoryId, true);
    247                         MainFrame.getInstance().getProgressBar().setValue(0);
    248                         ImagesManagement.sendingFiles = false;
    249                 }
    250         }
     236                    LOG.error(Outil.getStackTrace(e));
     237                    //displays a dialog if there is an error
     238                    JOptionPane.showMessageDialog(null, Messages.getMessage("sendingError") + files[i].getName(),
     239                            Messages.getMessage("error"), JOptionPane.ERROR_MESSAGE);
     240                    MainFrame.getInstance().setMessage(Messages.getMessage("sendingError") + " " + files[i].getName());
     241                } finally {
     242                    MainFrame.getInstance().getProgressBar().setValue(nbProgressBar);
     243                }
     244            }
     245            //refresh
     246            MainFrame.getInstance().setAdditionalMessage("");
     247            rafraichir(categoryId, true);
     248            MainFrame.getInstance().getProgressBar().setValue(0);
     249            imagesManagement.setSendingFiles(true);
     250        }
     251    }
    251252
    252253}
Note: See TracChangeset for help on using the changeset viewer.