Calling a ViewPart(SWT) from void main - eclipse-plugin

I am new to eclipse plugin and SWT. I am designing a view part with some functionalities.
But the only problem is that I have to Launch it as a eclipse plugin to check.
Is there any way that we can call this from that main(String[] args) method?
I am posting my sample viewpart code
public class View extends ViewPart {
public static Display display=new Display();
public static final String ID = "Test.View"; //$NON-NLS-1$
private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
public View() {
}
/**
* Create contents of the view part.
* #param parent
*/
#Override
public void createPartControl(Composite parent) {
Composite container = toolkit.createComposite(parent, SWT.NONE);
toolkit.paintBordersFor(container);
{
Label lblNewLabel = new Label(container, SWT.NONE);
lblNewLabel.setBounds(25, 46, 49, 13);
toolkit.adapt(lblNewLabel, true, true);
lblNewLabel.setText("New Label");
}
Spinner spinner = new Spinner(container, SWT.BORDER);
spinner.setBounds(88, 105, 47, 21);
toolkit.adapt(spinner);
toolkit.paintBordersFor(spinner);
DragSource dragSource = new DragSource(container, DND.DROP_MOVE);
DropTarget dropTarget = new DropTarget(container, DND.DROP_MOVE);
Canvas canvas = new Canvas(container, SWT.NONE);
canvas.setBounds(94, 161, 174, 148);
toolkit.adapt(canvas);
toolkit.paintBordersFor(canvas);
createActions();
initializeToolBar();
initializeMenu();
}
public void dispose() {
toolkit.dispose();
super.dispose();
}
/**
* Create the actions.
*/
private void createActions() {
// Create the actions
}
/**
* Initialize the toolbar.
*/
private void initializeToolBar() {
IToolBarManager tbm = getViewSite().getActionBars().getToolBarManager();
}
/**
* Initialize the menu.
*/
private void initializeMenu() {
IMenuManager manager = getViewSite().getActionBars().getMenuManager();
}
#Override
public void setFocus() {
// Set the focus
}
Can I write something like
public static void main(String[] args){
View v=new View();
Shell shell=new Shell(display);
v.createPartControl(shell);
}

Not really, Eclipse views are part of RCP and depend on a its infrastructure. You can create a small RCP application which only shows your view instead.
Having public static Display display=new Display(); in your view is completely wrong! There is certainly a Display already by the time your code runs, which can be accessed by Display.getDefault() (on any thread) or Display.getCurrent() (on GUI thread).

Related

How to change the window title bar color

I want to know how to develop the idea plug-in, how to use the window, or how to define the child window of the idea
public class EncryptionAndDecryption extends AnAction {
#Override
public void actionPerformed(#NotNull AnActionEvent e) {
System.out.println("加密解密");
new test();
}
}
public class test {
private JButton button1;
private JPanel panel1;
public test() {
JFrame jFrame = new JFrame("test");
jFrame.setContentPane(panel1);
jFrame.setSize(500, 400);
//jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
jFrame.setLocationRelativeTo(null);
// jFrame.setUndecorated(true);
jFrame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
jFrame.setVisible(true);
}
}
How to change title block color

Android App: Replace default button background with custom background in a Dialog Fragment

What I'm trying to do is change the default backgrounds of a custom DialogFragment that I have written. Normally, I would do this by changing the XML layout file, however, for a DialogFragment these buttons don't exist in the layout file.
In essence, I'm trying to get access to the setPositiveButton, setNegativeButton and setNeutral buttons in order to modify them. Alternatively, I would next try to do this by getting them by id, however, since they aren't defined in a layout file, I do not have a corresponding id for them. I've found plenty examples of how to modify the rest of the layout, but I can't find anywhere that positive/neutral/negative buttons are modifiable.
Ideally, I could do this in the following block of code:
.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
...
}
})
Thanks in advance.
Here is the code ... The button instance is valid only after the dialog created. Hope this helps you.
public static class CustomDialog extends DialogFragment
{
public static CustomDialog newInstance()
{
return new CustomDialog();
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
super.onCreateDialog(savedInstanceState);
Builder builder = new AlertDialog.Builder(getActivity());
AlertDialog dialog = builder.create();
dialog.setButton(DialogInterface.BUTTON_POSITIVE, "OK",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "CANCEL",new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return dialog;
}
#Override
public void onStart()
{
super.onStart();
Button pButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
Button nButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_NEGATIVE);
pButton.setBackgroundColor(getResources().getColor(R.color.Blue));
nButton.setBackgroundColor(getResources().getColor(R.color.Green));
}
}

Display JColorChooser as frame and pass color value to buttons

Hello All and Thanks in advance.
I need JColorChooser to appear as the top panel in my window. I used a button in its place for formatting purposes.(See picture) I also need to pass chosen color value from colorchooser to chosen button in array. I have most of the structure complete but I am still pretty new to Java.
** I would like to also note that I do not need 'ok' or 'cancel' buttons in the colorchooser dialog because once you select a color you should be able to press as many buttons as you would like to change their colors to selected color.
View an image of my program
package main;
public class Main {
public static JPanel ToolPanel = new JPanel();
public static JPanel GridPanel = new JPanel();// Define the Panel that Holds 256 Button Array
public static JPanel FullPanel = new JPanel();
public static JButton button[] = new JButton[256];// Define button array for 256 buttons
private static JColorChooser colorChooser;
public static JButton ColorChooserButton = new JButton("This is where the JColorChooser(colorChooser) should go. ");
public static JFrame MaineWindow = new JFrame("Kola Color Crapper");
final static String ToolPanelString = "Tool Panel";
final static String GridPanelString = "Grid Panel";
public static ActionListener ButtonArrayActionListener = new ActionListener() {
public void actionPerformed(ActionEvent aef) {
if (aef.getSource() instanceof JButton) {
((JButton) aef.getSource()).setBackground(Color.blue);}}};
public static ActionListener ColorChooserButtonActionListener = new ActionListener() {
public void actionPerformed(ActionEvent aeef) {
String st="Welcome";
JOptionPane.showMessageDialog(null,st);
}};
public static void main( String[] args ){//Main (Run)
// setup the gui with buttons
CreateFullPanel();
CreateMaineWindow();
}
//CreateToolPanel() is just used to test formatting
public static void CreateToolPanel(){
ToolPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
ToolPanel.setLayout(new GridLayout(1,0));
ColorChooserButton.addActionListener(ColorChooserButtonActionListener);
ToolPanel.add(ColorChooserButton);
ColorChooserButton.setToolTipText("This is a tool tip.");
}
public static void CreateFullPanel(){
CreateToolPanel();
CreateGridPanel();
//I want to insert CreateColorChooserPanel() where CreateToolPanel() is
// CreateColorChooserPanel();
}
public static void CreateGridPanel() {
GridPanel.setBorder(BorderFactory.createLineBorder(Color.blue, 2));
GridPanel.setLayout(new GridLayout(16, 16, 0, 0));
CreateButtonArray();// Create array of buttons
}
public static void CreateButtonArray(){
for (int i = 0; i < button.length; i++) {
button[i] = new JButton(Integer.toString(i + 1));
button[i].addActionListener(ButtonArrayActionListener);
GridPanel.add(button[i]);
GridPanel.setToolTipText("ToolTIp does not work");
}
}
public static void CreateMaineWindow(){
MaineWindow.setLayout(new GridLayout(2,1));//Main Window is resizable
MaineWindow.add(ToolPanel, ToolPanelString);
MaineWindow.add(GridPanel, ToolPanelString);
MaineWindow.setBounds(300,300,600,400);
MaineWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MaineWindow.pack();// pack all the contents of Main Window
MaineWindow.setVisible(true);//Show Main Window
}
public static void CreateColorChooserPanel(){
}
}

GWT popup is not centered when built within onClickHandler

My aim is to use GWT.runSync to load the popup contents only when required.
If I construct my widget as:
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
popupPanel.center();
}
});
}
}
Then the popup will be centered correctly.
If I build the popup within the clickHandler:
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
#Override
public void onClick(ClickEvent event) {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
popupPanel.center();
}
});
}
}
The popup will not center correctly. I have tried using setPositionAndShow, however the supplied offsets are 12, even though the CreateWidget is actually about 200px for both width and height.
I want to use the second method so I can eventually use GWT.runAsync within the onClick as CreateWidget is very complex.
I am using GWT-2.1.1
Seems to work by delaying the call to center. Perhaps a once off Timer would work as well. Delaying the call also works when wrapping buildUI within GWT.runAsync
public class CreateButton extends Button {
public CreateButton() {
super("Create");
buildUI();
}
private void buildUI() {
#Override
public void onClick(ClickEvent event) {
final CreateWidget createWidget = new CreateWidget();
final PopupPanel popupPanel = new PopupPanel(false);
popupPanel.setWidget(createWidget);
popupPanel.setGlassEnabled(true);
popupPanel.setAnimationEnabled(true);
addClickHandler(new ClickHandler() {
Scheduler.get().scheduleFixedDelay(new RepeatingCommand() {
#Override
public boolean execute() {
popupPanel.center();
return false;
}
}, 50); //a value greater than 50 maybe needed here.
});
}
}
}
}

SurfaceView Tutorial problems

I found a tutorial and it looks like this:
package com.djrobotfreak.SVTest;
public class Tutorial2D extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(_scratch, 10, 10, null);
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
#Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
and it does not work, no matter what I do. I am trying to convert my code to surfaceview but I cant find any surfaceview programs that even work (besides the android-provided ones). Does anyone know what the error even is saying?
Here is my logcat info: http://shrib.com/oJB5Bxqs
If you get a ClassNotFoundException, you should check the Manifest file.
Click on the Application tab and look on the botton right side under "Attributes for".
If there is a red X mark under your Class Name, then click on the "Name" link and locate the correct class to load.