Tab4Utils.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
omschaub.azcvsupdater.main |
![]() |
![]() |
AZCVSUpdater |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Created on Feb 6, 2005
* Created by omschaub
*
*/
package omschaub.azcvsupdater.main;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.Collator;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import omschaub.azcvsupdater.utilities.ButtonStatus;
import omschaub.azcvsupdater.utilities.ColorUtilities;
import omschaub.azcvsupdater.utilities.DirectoryUtils;
import omschaub.azcvsupdater.utilities.ShellUtils;
import omschaub.azcvsupdater.utilities.Time;
import omschaub.azcvsupdater.utilities.ZipUtils;
import omschaub.azcvsupdater.utilities.imagerepository.ImageRepository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
/**
* Tab4 Utilites
*/
public class Tab4Utils {
static int escPressed, crPressed;
static String destinationDirEnding;
static private boolean backupButtonStatus;
public static boolean makeBackup(File dir, String sourcedir, String destinationdir, String destinationDirEnding, boolean autoInitialized) {
boolean success = false;
String[] files;
files = dir.list();
File checkdir = new File(destinationdir + System.getProperty("file.separator") + destinationDirEnding);
if (!checkdir.isDirectory()) {
checkdir.mkdir();
};
Date date = new Date();
long msec = date.getTime();
checkdir.setLastModified(msec);
try{
for (int i = 0; i < files.length; i++) {
File f = new File (dir, files[i]);
File g = new File (files[i]);
if (f.isDirectory()){
}else if(f.getName().endsWith("saving")){
//StatusBoxUtils.mainStatusAdd(" Skipped " + f.getName(),0);
//Skipping any .saving files here
}
else {
if(f.canRead()){
String destinationFile = checkdir + System.getProperty("file.separator") + g;
String sourceFile = sourcedir + System.getProperty("file.separator") + g;
FileInputStream infile = new FileInputStream(sourceFile);
FileOutputStream outfile = new FileOutputStream(destinationFile);
int c;
while ((c = infile.read()) != -1)
outfile.write(c);
infile.close();
outfile.close();
}else{
System.out.println(f.getName() + " is LOCKED!");
while(!f.canRead()){
//spin wildly here
}
String destinationFile = checkdir + System.getProperty("file.separator") + g;
String sourceFile = sourcedir + System.getProperty("file.separator") + g;
FileInputStream infile = new FileInputStream(sourceFile);
FileOutputStream outfile = new FileOutputStream(destinationFile);
int c;
while ((c = infile.read()) != -1)
outfile.write(c);
infile.close();
outfile.close();
}
}
}
success = true;
} catch(Exception e) {
success = false;
e.printStackTrace();
}
if(autoInitialized){
Display display = View.getDisplay();
if(display != null || !display.isDisposed()){
View.getDisplay().syncExec( new Runnable (){
public void run() {
//backup_loadDirectory();
Tab4.redrawBackupTable();
}
});
}
return success;
}else{
View.getDisplay().syncExec( new Runnable (){
public void run() {
StatusBoxUtils.mainStatusAdd(" Backup Complete",1);
View.getPluginInterface().getPluginconfig().setPluginParameter
(
"Azcvsupdater_last_backup",
Time.getCurrentTime(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("MilitaryTime"))
);
Tab4.lastBackupTime = View.getPluginInterface().getPluginconfig().getPluginStringParameter("Azcvsupdater_last_backup");
if(Tab4.lastbackupValue != null || !Tab4.lastbackupValue.isDisposed()){
Tab4.lastbackupValue.setText("Last backup: " + Tab4.lastBackupTime);
}
//backup_loadDirectory();
Tab4.redrawBackupTable();
Tab6Utils.refreshLists();
}
});
return success;
}
}
/*public static void backup_loadDirectory() {
//String installdirectory = DirectoryUtils.getInstallDirectory();
final String backupinstalldirectory = DirectoryUtils.getBackupDirectory();
final String backupDirectory = backupinstalldirectory + System.getProperty("file.separator") + "config";
//set the directory on the label
if(View.getDisplay() == null && View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run () {
if (Tab4.directoryValue !=null && !Tab4.directoryValue.isDisposed()){
Tab4.directoryValue.setText("AZCVSUpdater Backup Directory: " + backupinstalldirectory);
}
}
});
final Thread backup_loadDirectory_thread = new Thread() {
public void run() {
//Working on files ... it's allways better to use a try / catch :)
try {
File f = new File(backupDirectory);
if(f.isFile()){
f.delete();
}
if(!f.isDirectory()){
f.mkdir();
}
File[] files = f.listFiles();
for(int i = 0 ; i < files.length ; i++) {
String fileName = files[i].getName();
if(!files[i].isDirectory())
continue;
long lastModified = files[i].lastModified();
addTableElement_backup(fileName,lastModified,Tab4.backupTable);
}
if(View.getDisplay()==null && View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run () {
sortTable(View.getPluginInterface().getPluginconfig().getPluginIntParameter("backup_sort",1));
}
});
//We also need to re-enable the browse button
ButtonStatus.set(true, true, false, true, true);
} catch(Exception e) {
//Stop process and trace the exception
e.printStackTrace();
}
}
};
// Before starting our Thread, we remove all elements in the table
if(View.getDisplay() == null && View.getDisplay().isDisposed())
return;
View.getDisplay().syncExec(new Runnable (){
public void run () {
if (Tab4.backupTable !=null && !Tab4.backupTable.isDisposed()){
Tab4.backupTable.removeAll();
}
backup_loadDirectory_thread.setDaemon(true);
backup_loadDirectory_thread.start();
}
});
}
*/
/*public static void addTableElement_backup(final String fileName,final long lastModified, final Table table_to_add) {
//tableCounter = false;
//As this method isn't called from the SWT Thread
//(but, in our example from the created 't' Thread)
//We need to 'post' GUI processing to the Display
//We check if display isn't null, and not disposed
if(View.getDisplay() == null || View.getDisplay().isDisposed())
return;
//And the 'magic' method :D
View.getDisplay().asyncExec( new Runnable() {
public void run() {
//Some time has passed, and so the table might be disposed
//We must check for it
if(table_to_add == null || table_to_add.isDisposed())
return;
TableItem item = new TableItem(table_to_add,SWT.NULL);
if (table_to_add.getItemCount()%2==0) {
item.setBackground(ColorUtilities.getBackgroundColor());
}
// folder icon
if(View.getDisplay() == null || View.getDisplay().isDisposed())
return;
item.setImage(0,ImageRepository.getImage("folder"));
item.setText(1,fileName);
// We use a Calendar to format the Date
Date when = new Date(lastModified);
SimpleDateFormat sdf;
if(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("MilitaryTime"))
{
sdf = new SimpleDateFormat("dd/MM/yyy HH:mm:ss");
}
else
{
sdf = new SimpleDateFormat("MM/dd/yyy hh:mm:ss aa" );
}
sdf.setTimeZone(TimeZone.getDefault());
String date = sdf.format(when);
item.setText(2,date);
//final CommentMaker comment_maker = new CommentMaker();
}
});
}*/
public static void deleteMultiDirs(String[] dirs_to_die){
String file_names = "";
File[] dirFiles = new File[dirs_to_die.length];
for(int i = 0; i < dirs_to_die.length ; i++){
File isDir = new File(dirs_to_die[i]);
if(!isDir.isDirectory())
return;
file_names = file_names + "\n" + isDir.getName();
}
Shell shell = new Shell();
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.NO | SWT.YES);
messageBox.setText("Delete Confirmation");
messageBox.setMessage("Are you sure you want to delete the following directories?" + file_names + "\nand all of their configuration files?");
int response = messageBox.open();
switch (response){
case SWT.YES:
try{
for(int j = 0 ; j < dirs_to_die.length ; j++){
dirFiles[j] = new File(dirs_to_die[j]);
File[] dirListing = dirFiles[j].listFiles();
for(int i = 0; i < dirListing.length ; i ++) {
if(dirListing[i].isFile())
dirListing[i].delete();
//StatusBoxUtils.mainStatusAdd(" File " + dirListing[i] + " deleted",0);
}
dirFiles[j].delete();
StatusBoxUtils.mainStatusAdd(" Directory " + dirFiles[j].getPath() + " deleted",0);
}
//backup_loadDirectory();
Tab4.redrawBackupTable();
}catch (Exception e){
e.printStackTrace() ;
}
if(View.getDisplay() == null || View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run (){
if(Tab4.backupTable != null && !Tab4.backupTable.isDisposed()){
Tab4.backupTable.deselectAll();
}
}
});
shell.dispose();
break;
case SWT.NO:
//loadDirectory(type);
if(View.getDisplay() == null || View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run () {
StatusBoxUtils.mainStatusAdd(" Directory Delete Cancelled",0);
if(Tab4.backupTable != null && !Tab4.backupTable.isDisposed()){
Tab4.backupTable.deselectAll();
}
if(Tab4.deleteBackup != null && !Tab4.deleteBackup.isDisposed()){
Tab4.deleteBackup.setEnabled(false);
}
}
});
shell.dispose();
break;
}
}
public static void deleteSelectedDir(String dirtodie) {
File timetodie = new File(dirtodie);
if (!timetodie.isDirectory())
return;
File[] dirListing = timetodie.listFiles();
Shell shell = new Shell();
MessageBox messageBox = new MessageBox(shell, SWT.ICON_QUESTION | SWT.NO | SWT.YES);
messageBox.setText("Delete Confirmation");
messageBox.setMessage("Are you sure you want to delete \n" + timetodie.getName() + "\nand all of its files?");
int response = messageBox.open();
switch (response){
case SWT.YES:
try{
for(int i = 0; i < dirListing.length ; i ++) {
if(dirListing[i].isFile())
dirListing[i].delete();
//StatusBoxUtils.mainStatusAdd(" File " + dirListing[i] + " deleted",0);
}
if(timetodie.isDirectory())
timetodie.delete();
StatusBoxUtils.mainStatusAdd(" Directory " + timetodie + " deleted",0);
//backup_loadDirectory();
Tab4.redrawBackupTable();
Tab4.deleteBackup.setEnabled(false);
}catch (Exception e){
e.printStackTrace() ;
}
if(View.getDisplay() == null || View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run (){
//StatusBoxUtils.mainStatusAdd(" Directory Deleted",0);
if(Tab4.backupTable != null && !Tab4.backupTable.isDisposed()){
Tab4.backupTable.deselectAll();
}
}
});
shell.dispose();
break;
case SWT.NO:
//loadDirectory(type);
if(View.getDisplay() == null || View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run () {
StatusBoxUtils.mainStatusAdd(" Directory Delete Cancelled",0);
if(Tab4.backupTable != null && !Tab4.backupTable.isDisposed()){
Tab4.backupTable.deselectAll();
}
if(Tab4.deleteBackup != null && !Tab4.deleteBackup.isDisposed()){
Tab4.deleteBackup.setEnabled(false);
}
}
});
shell.dispose();
break;
}
}
public static boolean backupButtonRun(int autoInitialized)
{
boolean success = false;
final String pluginDir2 = View.getPluginInterface().getUtilities().getAzureusUserDir();
File directory = new File(pluginDir2);
if (!directory.isDirectory()) {
StatusBoxUtils.mainStatusAdd("ERROR! File is a directory!?!",2);
return false;
}
else {
//String installdirectory = DirectoryUtils.getInstallDirectory();
String backupinstalldirectory = DirectoryUtils.getBackupDirectory()+ System.getProperty("file.separator") + "config";
try {
if (autoInitialized == 0){
directoryQuestion(directory,pluginDir2);
//makeBackup(directory,pluginDir2,backupinstalldirectory,destinationDirEnding);
//config_getter.setPluginParameter("Azcvsupdater_last_backup", azuputils.getCurrentTime());
//date_writer.setDateFile(pluginInterface,date_writer.getCurrentTime());
} else {
if(makeBackup(directory,pluginDir2,backupinstalldirectory,("ConfigAutoBackup" + View.getPluginInterface().getAzureusVersion()),true)){
success = true;
}
}
View.getPluginInterface().getPluginconfig().setPluginParameter("Azcvsupdater_last_backup", Time.getCurrentTime(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("MilitaryTime")));
} catch (Exception e1) {
success = false;
e1.printStackTrace();
}
}
return success;
}
public static boolean directoryQuestion(final File directory, final String pluginDir2){
backupButtonStatus = false;
destinationDirEnding = ("ConfigBackup" + View.getPluginInterface().getAzureusVersion());
// Shell Initialize
if(View.getDisplay()==null && View.getDisplay().isDisposed())
return false;
final Shell shell = new Shell(View.getDisplay(),SWT.DIALOG_TRIM);
shell.setImage(ImageRepository.getImage("folder"));
//Grid Layout
GridLayout layout = new GridLayout();
layout.numColumns = 1;
shell.setLayout(layout);
//composite for shell
Composite backup_composite = new Composite(shell,SWT.NULL);
//Grid Layout
layout = new GridLayout();
layout.numColumns = 3;
backup_composite.setLayout(layout);
//shell title
shell.setText("Directory Name");
//Text Line 1
Label nameLabel = new Label(backup_composite, SWT.NONE);
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
nameLabel.setLayoutData( gridData );
nameLabel.setText("Input Backup Configuration Directory Name");
//Input field
final Combo line1 = new Combo(backup_composite,SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
line1.setLayoutData( gridData);
//line1.setEditable( true );
//bring in suggested name and directory listing
line1.add(destinationDirEnding);
String configInstallDirectory = DirectoryUtils.getBackupDirectory() + System.getProperty("file.separator") + "config";
try {
File f = new File(configInstallDirectory);
File[] files = f.listFiles();
for(int i = 0 ; i < files.length ; i++) {
String fileName = files[i].getName();
if(files[i].isDirectory() )
{
if(!fileName.equalsIgnoreCase(destinationDirEnding)){
line1.add(fileName);
}
}
}
} catch(Exception e) {
//Stop process and trace the exception
e.printStackTrace();
}
line1.select(0);
//Button for Commit Changes
Button commit = new Button(backup_composite, SWT.PUSH);
gridData = new GridData(GridData.CENTER);
gridData.horizontalSpan = 1;
commit.setLayoutData( gridData);
commit.setText( "Accept Directory Name");
commit.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
destinationDirEnding = line1.getText();
if (line1.getText() == null){
line1.setText("Cannot Be Empty");
}
else {
//shell.close() ;
shell.dispose();
String backupinstalldirectory = DirectoryUtils.getBackupDirectory()+ System.getProperty("file.separator") + "config";
backupButtonStatus = Tab4Utils.makeBackup(directory,pluginDir2,backupinstalldirectory,destinationDirEnding,false);
String[] files = directory.list();
String[] file_to_zip = new String[files.length];
try{
for (int i = 0; i < files.length; i++){
File tempFile = new File(directory.getAbsolutePath() + System.getProperty("file.separator") + files[i]);
if(!tempFile.isDirectory() && !tempFile.getName().endsWith(".bak") &&
!tempFile.getName().endsWith(".saving")){
file_to_zip[i] = directory.getAbsolutePath() + System.getProperty("file.separator")+ files[i];
}
}
}catch(Exception f){
f.printStackTrace();
}
/*ZipUtils zipU = new ZipUtils();
zipU.makeBackupToZip(file_to_zip ,backupinstalldirectory,"test.zip");*/
//System.out.println("Manual Run of Backup using directoryQuestion is " + backupButtonStatus);
View.getPluginInterface().getPluginconfig().setPluginParameter("Azcvsupdater_last_backup",
Time.getCurrentTime(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("MilitaryTime")));
}
}
});
// Button for Cancel
Button cancel = new Button(backup_composite, SWT.PUSH);
gridData = new GridData(GridData.CENTER);
gridData.horizontalSpan = 2;
cancel.setLayoutData( gridData);
cancel.setText( "Cancel");
cancel.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
destinationDirEnding = null;
//shell.close() ;
if(View.getDisplay()==null && View.getDisplay().isDisposed())
return;
View.getDisplay().asyncExec(new Runnable (){
public void run () {
StatusBoxUtils.mainStatusAdd(" Backup Cancelled",0);
}
});
shell.dispose();
return;
}
});
ShellUtils.centerShellandOpen(shell);
//Listener for shell
line1.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
switch (e.character){
case SWT.ESC: escPressed=1;break;
case SWT.CR: crPressed=1;break;
}
if (escPressed == 1){
//escPressed = 0;
//shell.dispose() ;
}
}
public void keyReleased (KeyEvent e) {
if (escPressed == 1){
//shell.close();
escPressed = 0;
//System.out.println("Key Pressed");
shell.dispose();
}
if (crPressed == 1)
{
crPressed = 0;
destinationDirEnding = line1.getText();
if (line1.getText() == null)
{
line1.setText("Cannot Be Empty");
}
else
{
shell.dispose();
String backupinstalldirectory = DirectoryUtils.getBackupDirectory()+ System.getProperty("file.separator") + "config";
backupButtonStatus = makeBackup(directory,pluginDir2,backupinstalldirectory,destinationDirEnding,false);
System.out.println("backupButtonStatus Run " + backupButtonStatus);
View.getPluginInterface().getPluginconfig().setPluginParameter("Azcvsupdater_last_backup",
Time.getCurrentTime(View.getPluginInterface().getPluginconfig().getPluginBooleanParameter("MilitaryTime")));
}
}
}
});
return backupButtonStatus;
}
/**
* New sorting and memory for the Backup table
* @param int 1 = Column 1 sort Ascending
* @param int 2 = Column 1 sort Descending
* @param int 3 = Column 2 sort Ascending
* @param int 4 = Column 2 sort Descending
*/
/*
public static void sortTable(int column){
//setting up the memory for this feature
View.getPluginInterface().getPluginconfig().setPluginParameter("backup_sort",column);
//sort column 1
if(column == 1){
TableItem[] items = Tab4.backupTable.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(1);
for (int j = 0; j < i; j++){
String value2 = items[j].getText(1);
if (collator.compare(value1, value2) < 0) {
String[] values = {items[i].getText(1), items[i].getText(2)};
items[i].dispose();
TableItem item = new TableItem(Tab4.backupTable, SWT.NONE, j);
item.setText(1,values[0]);
item.setText(2,values[1]);
item.setImage(0,ImageRepository.getImage("folder"));
items = Tab4.backupTable.getItems();
break;
}
}
}
//reGray();
}else if(column == 2){
//sorting for column1 descending
TableItem[] items = Tab4.backupTable.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(1);
for (int j = 0; j < i; j++){
String value2 = items[j].getText(1);
if (collator.compare(value2, value1) < 0) {
String[] values = {items[i].getText(1), items[i].getText(2)};
items[i].dispose();
TableItem item = new TableItem(Tab4.backupTable, SWT.NONE, j);
item.setText(1,values[0]);
item.setText(2,values[1]);
item.setImage(0,ImageRepository.getImage("folder"));
items = Tab4.backupTable.getItems();
break;
}
}
}
//reGray();
}else if(column == 3){
//sorting for column3 ascending
TableItem[] items = Tab4.backupTable.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(2);
//look for the 0 and delete it if it is there
if((value1.substring(value1.indexOf(" ")+1,value1.indexOf(" ") + 2)).equalsIgnoreCase("0")){
value1 = value1.substring(0,value1.indexOf(" ") + 1) + value1.subSequence(value1.indexOf(" ") + 2, value1.length());
}
for (int j = 0; j < i; j++){
String value2 = items[j].getText(2);
if((value2.substring(value2.indexOf(" ")+1,value2.indexOf(" ") + 2)).equalsIgnoreCase("0")){
value2 = value2.substring(0,value2.indexOf(" ") + 1) + value2.subSequence(value2.indexOf(" ") + 2, value2.length());
}
if (collator.compare(value1, value2) < 0) {
String[] values = {items[i].getText(1), items[i].getText(2)};
items[i].dispose();
TableItem item = new TableItem(Tab4.backupTable, SWT.NONE, j);
item.setText(1,values[0]);
item.setText(2,values[1]);
item.setImage(0,ImageRepository.getImage("folder"));
items = Tab4.backupTable.getItems();
break;
}
}
}
// reGray();
}else if(column == 4){
//sorting for column3 descending
TableItem[] items = Tab4.backupTable.getItems();
Collator collator = Collator.getInstance(Locale.getDefault());
for (int i = 1; i < items.length; i++) {
String value1 = items[i].getText(2);
//look for the 0 and delete it if it is there
if((value1.substring(value1.indexOf(" ")+1,value1.indexOf(" ") + 2)).equalsIgnoreCase("0")){
value1 = value1.substring(0,value1.indexOf(" ") + 1) + value1.subSequence(value1.indexOf(" ") + 2, value1.length());
}
for (int j = 0; j < i; j++){
String value2 = items[j].getText(2);
if((value2.substring(value2.indexOf(" ")+1,value2.indexOf(" ") + 2)).equalsIgnoreCase("0")){
value2 = value2.substring(0,value2.indexOf(" ") + 1) + value2.subSequence(value2.indexOf(" ") + 2, value2.length());
}
if (collator.compare(value2, value1) < 0) {
String[] values = {items[i].getText(1), items[i].getText(2)};
items[i].dispose();
TableItem item = new TableItem(Tab4.backupTable, SWT.NONE, j);
item.setText(1,values[0]);
item.setText(2,values[1]);
item.setImage(0,ImageRepository.getImage("folder"));
items = Tab4.backupTable.getItems();
break;
}
}
}
//reGray();
}
}
*/
/* public static void reGray(){
TableItem[] items = Tab4.backupTable.getItems();
for(int i = 0; i < items.length ; i++){
if(i%2!=0){
items[i].setBackground(ColorUtilities.getBackgroundColor());
}
}
}
*/
//EOF
}
The table below shows all metrics for Tab4Utils.java.




