/***************************************************************************
* Copyright 2001-2009 The VietSpider All rights reserved. *
**************************************************************************/
package org.vietspider.ui.htmlexplorer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Text;
import org.vietspider.generic.ColorCache;
import org.vietspider.ui.services.ImageLoader;
import org.vietspider.ui.widget.AlphaDialog;
import org.vietspider.ui.widget.ImageHyperlink;
import org.vietspider.ui.widget.action.HyperlinkAdapter;
import org.vietspider.ui.widget.action.HyperlinkEvent;
/**
* Author : Nhu Dinh Thuan
* nhudinhthuan@yahoo.com
* Jul 7, 2009
*/
public class AddPathButton extends AlphaDialog {
private ImageHyperlink add;
private Image addNormal, addDown;
private NodeEditor2 _editor;
private HtmlExplorerActionListener _explorer;
public AddPathButton(HtmlExplorerActionListener explorer, NodeEditor2 editor) {
super(editor.getShell(), SWT.NO_TRIM | SWT.ON_TOP | SWT.BORDER);
this._editor = editor;
this._explorer = explorer;
displayTime = 5*1000;
popup.setBackgroundMode(SWT.INHERIT_DEFAULT);
Color borderColor = ColorCache.getInstance().getColor(200, 200, 200);
popup.setBackground(borderColor);
_editor.getShell().addShellListener(new ShellAdapter() {
@SuppressWarnings("unused")
public void shellDeiconified(ShellEvent e) {
fadeOut();
}
});
_editor.getTextComponent().addFocusListener(new FocusAdapter() {
@SuppressWarnings("unused")
public void focusGained(FocusEvent e) {
computeShowArea(_editor.getTextComponent());
}
@SuppressWarnings("unused")
public void focusLost(FocusEvent e) {
fadeOut();
}
});
GridLayout gridLayout = new GridLayout(1, false);
gridLayout.marginHeight = 0;
gridLayout.marginBottom = 1;
gridLayout.marginTop = 0;
gridLayout.marginLeft = 0;
gridLayout.marginRight = 0;
gridLayout.horizontalSpacing = 0;
gridLayout.verticalSpacing = 0;
gridLayout.marginWidth = 0;
popup.setLayout(gridLayout);
add = new ImageHyperlink(popup, SWT.CENTER);
ImageLoader imageLoader = new ImageLoader();
addNormal = imageLoader.load(popup.getDisplay(), "add2.png");
addDown = imageLoader.load(popup.getDisplay(), "add3.png");
add.setImage(addNormal);
add.addHyperlinkListener(new HyperlinkAdapter(){
@SuppressWarnings("unused")
public void linkEntered(HyperlinkEvent e) {
add.setImage(addNormal);
add.redraw();
}
@SuppressWarnings("unused")
public void linkExited(HyperlinkEvent e) {
add.setImage(addNormal);
add.redraw();
}
@SuppressWarnings("unused")
public void linkActivated(HyperlinkEvent e) {
add.setImage(addNormal);
add.redraw();
}
});
add.addMouseListener(new MouseAdapter() {
@SuppressWarnings("unused")
public void mouseUp(MouseEvent e) {
_explorer.addEditItem();
}
@SuppressWarnings("unused")
public void mouseDown(MouseEvent e) {
add.setImage(addDown);
add.redraw();
}
});
popup.pack();
}
public void computeShowArea(Text text) {
Display display = text.getDisplay();
popup.setBackground(ColorCache.getInstance().getColor(100, 100, 100));
Rectangle parentRect = display.map (text.getParent(), null, text.getBounds ());
Rectangle displayRect = text.getBounds();
int x = parentRect.x + displayRect.width - popup.getSize().x - 25;
int y = parentRect.y + displayRect.height - popup.getSize().y - 2 ;
//+ text.getCaretLocation().y;//- comboSize.y;
popup.setLocation(x, y);
popup.setAlpha(getFinalAlpha());
popup.setVisible(true);
}
}