/*
* Jampa
* Copyright (C) 2008-2009 J. Devauchelle and contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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.
*/
package org.jampa.gui.components.osd;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.program.Program;
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.Link;
import org.eclipse.swt.widgets.Listener;
import org.jampa.gui.translations.Messages;
import org.jampa.utils.Constants;
public class NewVersionOSD extends AbstractOSD {
private String _text;
private String _urlText;
private String _url;
public NewVersionOSD(String text, String urlText, String url) {
super();
_text = text;
_urlText = urlText;
_url = url;
}
protected void buildUI() {
super.buildUI();
final Composite inner = new Composite(_shell, SWT.NONE);
GridLayout gl = new GridLayout(2, false);
gl.marginLeft = 5;
gl.marginTop = 0;
gl.marginRight = 5;
gl.marginBottom = 5;
inner.setLayout(gl);
_shell.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event e) {
try {
// get the size of the drawing area
Rectangle rect = _shell.getClientArea();
// create a new image with that size
Image newImage = new Image(Display.getDefault(), Math.max(1, rect.width), rect.height);
// create a GC object we can use to draw with
GC gc = new GC(newImage);
// fill background
gc.setForeground(_bgFgGradient);
gc.setBackground(_bgBgGradient);
gc.fillGradientRectangle(rect.x, rect.y, rect.width, rect.height, true);
// draw shell edge
gc.setLineWidth(2);
gc.setForeground(_borderColor);
gc.drawRectangle(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);
// remember to dipose the GC object!
gc.dispose();
// now set the background image on the shell
_shell.setBackgroundImage(newImage);
// remember/dispose old used iamge
if (_oldImage != null) {
_oldImage.dispose();
}
_oldImage = newImage;
} catch (Exception err) {
err.printStackTrace();
}
}
});
GC gc = new GC(_shell);
String lines[] = _text.split("\n");
Point longest = null;
int typicalHeight = gc.stringExtent("X").y;
for (String line : lines) {
Point extent = gc.stringExtent(line);
if (longest == null) {
longest = extent;
continue;
}
if (extent.x > longest.x) {
longest = extent;
}
}
gc.dispose();
int minHeight = typicalHeight * lines.length;
CLabel imgLabel = new CLabel(inner, SWT.NONE);
imgLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
imgLabel.setImage(_informationImage);
CLabel titleLabel = new CLabel(inner, SWT.NONE);
titleLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_CENTER));
titleLabel.setText(Constants.APP_NAME);
titleLabel.setForeground(_titleFgColor);
Font f = titleLabel.getFont();
FontData fd = f.getFontData()[0];
fd.setStyle(SWT.BOLD);
_titleFont = new Font(null, fd);
titleLabel.setFont(_titleFont);
Label text = new Label(inner, SWT.WRAP);
GridData gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
//gd.horizontalAlignment = SWT.CENTER;
text.setLayoutData(gd);
text.setForeground(_fgColor);
text.setText(_text);
Link link = new Link(inner, SWT.NONE);
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
//gd.horizontalAlignment = SWT.CENTER;
link.setLayoutData(gd);
link.setForeground(_fgColor);
link.setText(_urlText);
link.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
Program.launch(_url);
}
});
Link closeLink = new Link(inner, SWT.NONE);
gd = new GridData(GridData.FILL_BOTH);
gd.horizontalSpan = 2;
gd.horizontalAlignment = SWT.CENTER;
closeLink.setLayoutData(gd);
closeLink.setText("<a>" + Messages.getString("NewVersionOSD.CloseLink") + "</a>");
closeLink.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
_closed = true;
moveShellsDown();
}
});
minHeight = 100;
_shell.setSize(350, minHeight);
if (Display.getDefault().getPrimaryMonitor() == null) { return; }
Rectangle clientArea = Display.getDefault().getPrimaryMonitor().getClientArea();
_locationX = clientArea.x + clientArea.width - _shell.getSize().x;
_locationY = clientArea.y + clientArea.height;
_targetLocationY = _locationY - _shell.getSize().y;
if (_isAlphaUsed) {
_locationY = _targetLocationY;
_currentAlpha = 0;
_shell.setAlpha(_currentAlpha);
}
// move other shells up
moveShellsUp();
_shell.setBounds(_locationX, _locationY, _shell.getSize().x, _shell.getSize().y);
_activeShells.add(_shell);
}
}