Tab2Utilities.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
omschaub.stuffer.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 Sep 30, 2005
* Created by omschaub
*
*/
package omschaub.stuffer.main;
import java.util.regex.*;
import java.util.Iterator;
import omschaub.stuffer.containers.ClientBlock;
import omschaub.stuffer.utilities.ImageRepository;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ColorDialog;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
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.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
public class Tab2Utilities {
private Shell errorShell;
public void addNewClient(){
final Thread addNew_thread = new Thread() {
public void run() {
if(Plugin.getDisplay()==null && Plugin.getDisplay().isDisposed())
return;
Plugin.getDisplay().asyncExec( new Runnable() {
public void run() {
// Shell Initialize
final Shell shell = new Shell(SWT.DIALOG_TRIM);
if(!Plugin.getPluginInterface().getUtilities().isOSX())
shell.setImage(ImageRepository.getImage("plus"));
//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("Add New Client");
//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 Name of Client you wish to block");
//Input field
final Text line1 = new Text(backup_composite,SWT.BORDER);
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
line1.setLayoutData( gridData);
//Radio buttons for Normal vs RegEx
Composite comp_buttons = new Composite(backup_composite,SWT.NULL);
comp_buttons.setLayout(new GridLayout(3,false));
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
comp_buttons.setLayoutData(gridData);
final Button choiceString = new Button(comp_buttons,SWT.RADIO);
choiceString.setText("Simple String (simple match)");
choiceString.setSelection(true);
final Button choiceRegEx = new Button(comp_buttons,SWT.RADIO);
choiceRegEx.setText("RegEx (complex match)");
//Color Comp
Composite color_comp = new Composite(backup_composite,SWT.NULL);
color_comp.setLayout(new GridLayout(3,false));
gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.horizontalSpan = 3;
color_comp.setLayoutData(gridData);
final Label labelcolor = new Label(color_comp,SWT.BORDER);
labelcolor.setBackground(Plugin.getDisplay().getSystemColor(SWT.COLOR_BLACK));
gridData = new GridData (GridData.FILL_HORIZONTAL);
gridData.widthHint = 50;
labelcolor.setLayoutData(gridData);
final Button color_choose = new Button(color_comp,SWT.PUSH);
color_choose.setText("Choose Color for Client");
color_choose.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
//Choose color
ColorDialog colorDialog1 = new ColorDialog(shell);
colorDialog1.setText("Choose Color for Client");
colorDialog1.setRGB(new RGB(0,0,0));
RGB selectedColor = colorDialog1.open();
if(selectedColor != null){
Color color = new Color(Plugin.getDisplay(),
selectedColor.red,
selectedColor.green,
selectedColor.blue);
labelcolor.setBackground(color);
color.dispose();
}
}
});
//Button for Accept
Button commit = new Button(backup_composite, SWT.PUSH);
gridData = new GridData(GridData.CENTER);
gridData.horizontalSpan = 1;
commit.setLayoutData( gridData);
commit.setText( "Accept");
commit.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
if(line1.getText().equalsIgnoreCase("")){
MessageBox mb = new MessageBox(Plugin.getDisplay().getActiveShell(),SWT.ICON_ERROR);
mb.setText("Error");
mb.setMessage("Please input the client name you wish to block.");
mb.open();
return;
}
if(!choiceString.getSelection() && !choiceRegEx.getSelection()){
MessageBox mb = new MessageBox(Plugin.getDisplay().getActiveShell(),SWT.ICON_ERROR);
mb.setText("Error");
mb.setMessage("Please select whether this new client's name is a regular string or a regex expression.");
mb.open();
return;
}else{
if(choiceString.getSelection()){
FileUtilities.writeNewClient(line1.getText(),"0","1","1","0","r"+labelcolor.getBackground().getRed() + "g" + labelcolor.getBackground().getGreen() + "b" + labelcolor.getBackground().getBlue());
}else{
try{
Pattern p = Pattern.compile(line1.getText());
Matcher m = p.matcher("Test");
m.find();
}catch(PatternSyntaxException f){
f.printStackTrace();
String text = ("Java is reporting that your RegEx is incorrectly formed, please correcct this. \n\n" +
"For further information and examples, check the readme at \n\n" +
"http://azcvsupdater.sourceforge.net/stuffer/readme.txt \n\n " +
"Also check http://www.regular-expressions.info for more information\n\n" +
"Java is reporting the following problem with the expression: \n"
+ f.getDescription()) + " at position " + f.getIndex() + " in the rule";
openErrorMessageBox("Error",text);
return;
}
FileUtilities.writeNewClient(line1.getText(),"1","1","1","0","r"+labelcolor.getBackground().getRed() + "g" + labelcolor.getBackground().getGreen() + "b" + labelcolor.getBackground().getBlue());
}
loadClientTable(-1);
shell.dispose();
}
}
});
// 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) {
shell.dispose();
}
});
line1.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
//Empty
}
public void keyReleased (KeyEvent e) {
switch (e.character){
case SWT.ESC:
shell.dispose();
break;
case SWT.CR:
if(line1.getText().equalsIgnoreCase("") || line1.getText().equalsIgnoreCase(" ")){
MessageBox mb = new MessageBox(Plugin.getDisplay().getActiveShell(),SWT.ICON_ERROR);
mb.setText("Error");
mb.setMessage("Please input the client name you wish to block.");
mb.open();
break;
}
if(!choiceString.getSelection() && !choiceRegEx.getSelection()){
MessageBox mb = new MessageBox(Plugin.getDisplay().getActiveShell(),SWT.ICON_ERROR);
mb.setText("Error");
mb.setMessage("Please select whether this new client's name is a regular string or a regex expression.");
mb.open();
break;
}else{
if(choiceString.getSelection()){
FileUtilities.writeNewClient(line1.getText(),"0","1","1","0","r"+labelcolor.getBackground().getRed() + "g" + labelcolor.getBackground().getGreen() + "b" + labelcolor.getBackground().getBlue());
}else{
FileUtilities.writeNewClient(line1.getText(),"1","1","1","0","r"+labelcolor.getBackground().getRed() + "g" + labelcolor.getBackground().getGreen() + "b" + labelcolor.getBackground().getBlue());
}
loadClientTable(-1);
shell.dispose();
}
break;
}
}
});
Utils.centerShellandOpen(shell);
}
});
}
};
addNew_thread.run();
}
public void loadClientTable(final int selection){
final Thread load_client_list_thread = new Thread() {
public void run() {
try {
if(Plugin.getDisplay()== null && Plugin.getDisplay().isDisposed())
return;
Plugin.getDisplay().syncExec(new Runnable (){
public void run () {
if (Plugin.getTab2().getClientTable() != null && !Plugin.getTab2().getClientTable().isDisposed()){
Control[] controls = Plugin.getTab2().getClientTable().getChildren();
for(int i = 0; i < controls.length ; i++){
controls[i].dispose();
controls[i] = null;
}
Plugin.getTab2().getClientTable().setEnabled(true);
Plugin.getTab2().getClientTable().removeAll();
}
}
});
FileUtilities.readClientList();
Iterator it = Plugin.clientBlock_set.getIterator();
while(it.hasNext()){
ClientBlock cb = (ClientBlock)it.next();
addTableElement(String.valueOf(cb.getIndex()),
cb.getClientName(),
(cb.get_isRegEx() ? "1" : "0"),
(cb.get_isDownloading() ? "1" : "0"),
(cb.get_isUploading() ? "1" : "0"),
(cb.get_isSuperseeding() ? "1" : "0"),
cb.getColor(),
cb.getHitsThisSession());
}
Plugin.getDisplay().syncExec(new Runnable (){
public void run () {
if (Plugin.getTab2().getClientTable() !=null && !Plugin.getTab2().getClientTable().isDisposed()){
if(selection > -1){
Plugin.getTab2().getClientTable().setSelection(selection);
}
}
}
});
/*String[][] clientList = FileUtilities.getClientList(true);
for(int i = 0 ; i < 200 ; i++) {
if(clientList[i][0] != null){
//add table element
addTableElement(clientList[i][0],
clientList[i][1],
clientList[i][2],
clientList[i][3],
clientList[i][4],
clientList[i][5],
clientList[i][6]);
}
}*/
} catch(Exception e) {
//Stop process and trace the exception
e.printStackTrace();
}
}
};
load_client_list_thread.setDaemon(true);
load_client_list_thread.start();
}
public void addTableElement(final String number,
final String name,
final String isRegEx,
final String isDownload,
final String isUpload,
final String isSuperSeed,
final String colorString,
final int hits){
if(Plugin.getDisplay() == null || Plugin.getDisplay().isDisposed())
return;
Plugin.getDisplay().asyncExec( new Runnable() {
public void run() {
if(Plugin.getTab2().getClientTable() == null || Plugin.getTab2().getClientTable().isDisposed())
return;
final int item_number = Plugin.getTab2().getClientTable().getItemCount();
final TableItem item = new TableItem(Plugin.getTab2().getClientTable(),SWT.NULL);
if (Plugin.getTab2().getClientTable().getItemCount()%2==0) {
item.setBackground(ColorUtilities.getBackgroundColor());
}
//set the initial hits
item.setText(7, String.valueOf(hits));
//column one is the number
item.setText(0,stringNumberUpOne(number));
// column two is the name
item.setText(1,name);
//rule Columns
TableEditor editor = new TableEditor(Plugin.getTab2().getClientTable());
final Combo isRegEx_combo = new Combo(Plugin.getTab2().getClientTable(),SWT.READ_ONLY);
final Button button_isDownload = new Button (Plugin.getTab2().getClientTable(), SWT.CHECK);
final Button button_isUpload = new Button (Plugin.getTab2().getClientTable(), SWT.CHECK);
final Button button_isSuperSeed = new Button (Plugin.getTab2().getClientTable(), SWT.CHECK);
final Label colorLabel = new Label (Plugin.getTab2().getClientTable(),SWT.BORDER);
editor = new TableEditor(Plugin.getTab2().getClientTable());
isRegEx_combo.add("String");
isRegEx_combo.add("RegEx");
isRegEx_combo.pack ();
isRegEx_combo.setBackground(item.getBackground());
editor.grabHorizontal = true;
editor.setEditor (isRegEx_combo, item, 2);
if(isRegEx.equalsIgnoreCase("1")){
isRegEx_combo.select(1);
isRegEx_combo.clearSelection();
}else{
isRegEx_combo.select(0);
isRegEx_combo.clearSelection();
}
isRegEx_combo.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Plugin.getTab2().getClientTable().setSelection(item_number);
String position = item.getText(0);
position = stringNumberDownOne(position);
String clientName = item.getText(1);
String[] oldRules = FileUtilities.getRules(position);
if(isRegEx_combo.getSelectionIndex() == 0){
FileUtilities.changeClientRules(position,clientName,"0",oldRules[1],oldRules[2],oldRules[3],oldRules[4]);
isRegEx_combo.clearSelection();
}else{
try{
Pattern p = Pattern.compile(item.getText(1));
Matcher m = p.matcher("Test");
m.find();
FileUtilities.changeClientRules(position,clientName,"1",oldRules[1],oldRules[2],oldRules[3],oldRules[4]);
isRegEx_combo.clearSelection();
}catch(PatternSyntaxException f){
f.printStackTrace();
String errorText = ("Java is reporting that your RegEx is incorrectly formed, please correct this. \n\n" +
"For further information and examples, check the readme at \n\n" +
"http://azcvsupdater.sourceforge.net/stuffer/readme.txt \n\n" +
"Also check http://www.regular-expressions.info for more information\n\n" +
"Java is reporting the following problem with the expression: \n"
+ f.getDescription()) + " at position " + f.getIndex() + " in the rule";
openErrorMessageBox("Error",errorText);
isRegEx_combo.select(0);
isRegEx_combo.clearSelection();
}
}
}
});
editor = new TableEditor(Plugin.getTab2().getClientTable());
button_isDownload.pack ();
button_isDownload.setBackground(item.getBackground());
editor.minimumWidth = button_isDownload.getSize ().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor (button_isDownload, item, 3);
if(isDownload.equalsIgnoreCase("1")){
button_isDownload.setSelection(true);
}else{
button_isDownload.setSelection(false);
}
button_isDownload.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Plugin.getTab2().getClientTable().setSelection(item_number);
String position = item.getText(0);
position = stringNumberDownOne(position);
String clientName = item.getText(1);
String[] oldRules = FileUtilities.getRules(position);
if(button_isDownload.getSelection()){
button_isSuperSeed.setSelection(false);
FileUtilities.changeClientRules(position,clientName,oldRules[0],"1",oldRules[2],"0",oldRules[4]);
}else{
FileUtilities.changeClientRules(position,clientName,oldRules[0],"0",oldRules[2],oldRules[3],oldRules[4]);
}
}
});
// Column three is isupload
editor = new TableEditor(Plugin.getTab2().getClientTable());
//button_isUpload = new Button (Tab2.clientTable, SWT.CHECK);
button_isUpload.pack ();
button_isUpload.setBackground(item.getBackground());
editor.minimumWidth = button_isUpload.getSize ().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor (button_isUpload, item, 4);
if(isUpload.equalsIgnoreCase("1")){
button_isUpload.setSelection(true);
}else{
button_isUpload.setSelection(false);
}
button_isUpload.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Plugin.getTab2().getClientTable().setSelection(item_number);
String position = item.getText(0);
position = stringNumberDownOne(position);
String clientName = item.getText(1);
String[] oldRules = FileUtilities.getRules(position);
if(button_isUpload.getSelection()){
button_isSuperSeed.setSelection(false);
FileUtilities.changeClientRules(position,clientName,oldRules[0],oldRules[1],"1","0",oldRules[4]);
}else{
FileUtilities.changeClientRules(position,clientName,oldRules[0],oldRules[1],"0",oldRules[3],oldRules[4]);
}
}
});
//Column four is isSuperSeed
editor = new TableEditor(Plugin.getTab2().getClientTable());
button_isSuperSeed.pack ();
button_isSuperSeed.setBackground(item.getBackground());
editor.minimumWidth = button_isSuperSeed.getSize ().x;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor (button_isSuperSeed, item, 5);
if(isSuperSeed.equalsIgnoreCase("1")){
button_isSuperSeed.setSelection(true);
}else{
button_isSuperSeed.setSelection(false);
}
button_isSuperSeed.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Plugin.getTab2().getClientTable().setSelection(item_number);
String position = item.getText(0);
position = stringNumberDownOne(position);
String clientName = item.getText(1);
String[] oldRules = FileUtilities.getRules(position);
if(button_isSuperSeed.getSelection()){
button_isDownload.setSelection(false);
button_isUpload.setSelection(false);
FileUtilities.changeClientRules(position,clientName,oldRules[0],"0","0","1",oldRules[4]);
}else{
FileUtilities.changeClientRules(position,clientName,oldRules[0],oldRules[1],oldRules[2],"0",oldRules[4]);
}
}
});
//Column six is color
editor = new TableEditor(Plugin.getTab2().getClientTable());
colorLabel.pack ();
colorLabel.setToolTipText("Double-click to change color");
Color temp_color = new Color(Plugin.getDisplay(),Utils.getRGB(colorString));
colorLabel.setBackground(temp_color);
temp_color.dispose();
editor.minimumWidth = 20;
editor.horizontalAlignment = SWT.CENTER;
editor.setEditor (colorLabel, item, 6);
colorLabel.addListener(SWT.MouseDown, new Listener() {
public void handleEvent(Event e) {
Plugin.getTab2().getClientTable().setSelection(item_number);
}
});
colorLabel.addListener(SWT.MouseDoubleClick, new Listener() {
public void handleEvent(Event e) {
//Choose color
Plugin.getTab2().getClientTable().setSelection(item_number);
ColorDialog colorDialog1 = new ColorDialog(colorLabel.getShell());
colorDialog1.setText("Choose Color for Client");
colorDialog1.setRGB(colorLabel.getBackground().getRGB());
RGB selectedColor = colorDialog1.open();
if(selectedColor != null){
String position = item.getText(0);
position = stringNumberDownOne(position);
String clientName = item.getText(1);
String[] oldRules = FileUtilities.getRules(position);
Color color = new Color(Plugin.getDisplay(),
selectedColor.red,
selectedColor.green,
selectedColor.blue);
colorLabel.setBackground(color);
color.dispose();
FileUtilities.changeClientRules(position,clientName,oldRules[0],oldRules[1],oldRules[2],oldRules[3],
"r"+colorLabel.getBackground().getRed() + "g" + colorLabel.getBackground().getGreen() + "b" + colorLabel.getBackground().getBlue());
}
}
});
}
});
}
/**
* Checks for view, and if present opens a message box
* @param title
* @param Message
*/
public void openErrorMessageBox(final String title, final String message){
final Thread errorbox_thread = new Thread() {
public void run() {
if(Plugin.getDisplay() == null && Plugin.getDisplay().isDisposed())
return;
Plugin.getDisplay().asyncExec( new Runnable() {
public void run() {
if(errorShell != null && !errorShell.isDisposed()){
if(errorShell.isVisible())
return;
}
errorShell = new Shell(Plugin.getDisplay(),SWT.DIALOG_TRIM);
//Grid Layout
GridLayout layout = new GridLayout();
layout.numColumns = 1;
errorShell.setLayout(layout);
//shell title
errorShell.setText(title);
//Text Line 1
Text text = new Text(errorShell, SWT.MULTI | SWT.BORDER);
GridData gridData = new GridData(GridData.FILL_BOTH);
text.setLayoutData( gridData );
text.setText(message);
text.pack();
// OK Button
Button ok = new Button(errorShell, SWT.PUSH);
gridData = new GridData(GridData.HORIZONTAL_ALIGN_END);
gridData.horizontalSpan = 2;
ok.setLayoutData( gridData);
ok.setText( " OK ");
ok.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
errorShell.dispose();
}
});
//Center and open box
Utils.centerShellandOpen(errorShell);
}
});
}
};
errorbox_thread.setDaemon(true);
errorbox_thread.start();
}
public String stringNumberDownOne(String position){
try{
int numberOfPosition = Integer.valueOf(position).intValue();
numberOfPosition--;
return String.valueOf(numberOfPosition);
}catch(Exception e){
e.printStackTrace();
return position;
}
}
public String stringNumberUpOne(String number){
try{
int numberOfPosition = Integer.valueOf(number).intValue();
numberOfPosition++;
String newNumber =String.valueOf(numberOfPosition);
//System.out.println(numberOfPosition + ":" + newNumber);
return newNumber;
}catch(Exception e){
e.printStackTrace();
return number;
}
}
/* public void renumberMOD_BAD(final boolean MOD, final boolean BAD){
if(Plugin.getDisplay() == null || Plugin.getDisplay().isDisposed()){
return;
}
Plugin.getDisplay().asyncExec(new Runnable (){
public void run () {
if(MOD){
if(Tab2.MOD_block_num != null || !Tab2.MOD_block_num.isDisposed()){
Tab2.MOD_block_num.setText(String.valueOf(Constants.MOD_CLIENT_BLOCKS));
Tab2.MOD_block_num.getParent().layout();
}
}
if(BAD){
if(Tab2.BAD_block_num != null || !Tab2.BAD_block_num.isDisposed()){
Tab2.BAD_block_num.setText(String.valueOf(Constants.BAD_CLIENT_BLOCKS));
Tab2.BAD_block_num.getParent().layout();
}
}
}
});
}*/
/**
* Change the number of Hits for a given client at index
* @param index
* @param state (0 for Change to 0, 1 for increase, 2 for decrease)
*/
public void changeHits(final int index, final int state, final ClientBlock cb){
try{
if(Plugin.getDisplay() != null && !Plugin.getDisplay().isDisposed()){
Plugin.getDisplay().asyncExec(new Runnable (){
public void run () {
if(Plugin.getTab2().getClientTable() == null || Plugin.getTab2().getClientTable().isDisposed())
return;
TableItem[] items = Plugin.getTab2().getClientTable().getItems();
items[index].setText(7, String.valueOf(cb.getHitsThisSession()));
}
});
}
}catch(Exception e){
e.printStackTrace();
}
}
}//EOF
The table below shows all metrics for Tab2Utilities.java.




