/*
* Copyright (C) 2015 Shashank Tulsyan <shashaank at neembuu.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package neembuu.uploader.updateprogress;
import java.awt.Component;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import neembuu.uploader.external.UpdateProgressUI;
import neembuu.uploader.external.UpdateProgressUI.Content;
import neembuu.uploader.theme.ThemeCheck;
import neembuu.uploader.translation.Translation;
/**
*
* @author Shashank Tulsyan <shashaank at neembuu.com>
*/
public class UpdateProgressImpl extends javax.swing.JFrame {
/**
* Creates new form UpdateProgressImpl
*/
public UpdateProgressImpl() {
initComponents();
}
@Override
public void setVisible(boolean b) {
ThemeCheck.apply(this);
super.setVisible(b); //To change body of generated methods, choose Tools | Templates.
}
public static final UpdateProgressImpl INSTANCE = new UpdateProgressImpl();
public final void updateTranslation(){
setTitle(Translation.T().updatingPlugins());
}
public UpdateProgressUI upui(){return upui;}
private final UpdateProgressUI upui = new UpdateProgressUI() {
@Override public Content addContent(String str) {
final ProgressingItem pi = new ProgressingItem();
pi.content.setText(str);
pi.progress.setValue(0);
contentsPanel.add(pi);jScrollPane1.revalidate();setVisible(true);
final Content c = new Content() {
@Override public void setString(String str2) {
pi.content.setText(str2);
}
@Override public void setProgress(double progress) {
progress = Math.max(0d,Math.min(1d,progress));
pi.progress.setValue((int)(progress*100));
if(progress==1d)done();
}
@Override public void done() {
SwingUtilities.invokeLater(new Runnable() {
@Override public void run() {
contentsPanel.remove(pi);
jScrollPane1.revalidate();
repaint(); Component[]c=contentsPanel.getComponents();
if(c==null || c.length==0) setVisible(false);
}});
}
};
/*Thread t = new Thread("Timeout Kill "+str){
@Override public void run() {
try{Thread.sleep(1000*20);}catch(Exception a){}c.done();
}
}; t.setDaemon(true); t.start();*/
return c;
}
};
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
contentsPanel = new javax.swing.JPanel();
setAlwaysOnTop(true);
setMinimumSize(new java.awt.Dimension(400, 175));
contentsPanel.setLayout(new javax.swing.BoxLayout(contentsPanel, javax.swing.BoxLayout.Y_AXIS));
jScrollPane1.setViewportView(contentsPanel);
getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
pack();
}// </editor-fold>//GEN-END:initComponents
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(UpdateProgressImpl.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(UpdateProgressImpl.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(UpdateProgressImpl.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(UpdateProgressImpl.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
UpdateProgressImpl impl =
new UpdateProgressImpl();
for (int i = 0; i < 10; i++) {
String nm = Integer.toHexString(
(int)(Math.random()*100000));
double progress = Math.random();
final Content c = impl.upui.addContent(nm);
c.setProgress(progress);
new Thread(){
@Override public void run() {
try {
Thread.sleep((int)(Math.random()*20*1000));
c.done();
} catch (InterruptedException ex) {
Logger.getLogger(UpdateProgressImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
}.start();
}
impl.setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel contentsPanel;
private javax.swing.JScrollPane jScrollPane1;
// End of variables declaration//GEN-END:variables
}