lundi 11 mai 2015

IResourceChangeListener rename and set EditorPart name dynamically

IResourceChangeListener listens to changes in project workspace for example if the editor part file name has changed.

I want to know how to access that particular EditorPart and change its title name accordingly (e.g. with .setPartName), or maybe the refresh the editor so that it shows the new name automatically.

Ideal would be if the IResourceChangeListener has a Rename event type but does not seem to be the case.

Eclipse Android Game - Crashes when I hit Play

I've been following some tutorials online to try to get a grasp of basic android development, and I seem to be stuck. There are no errors in my code, but a few warnings and whenever I press play from the main menu, it crashes. If anyone could help out that'd be greatly appreciated!!

This is my MainActivity.java

package com.example.shadowassassinstu3;

import com.example.shadowassassinstu3.Game;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void play(View v)
    {
        Intent i=new Intent(this,Game.class);
        startActivity(i);
    }

    public void exit(View v)
    {
        System.exit(0);
    }
}

This is my Game.java

package com.example.shadowassassinstu3;

import com.example.shadowassassinstu3.gameloop;
import com.example.shadowassassinstu3.Game.GameView;
import com.example.shadowassassinstu3.Game.TeleListener;
import com.example.shadowassassinstu3.R;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.Display;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class Game extends Activity {
    gameloop gameLoopThread;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //phone state
        TelephonyManager TelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        TelephonyMgr.listen(new TeleListener(),PhoneStateListener.LISTEN_CALL_STATE);
        //for no title
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new GameView(this));     
    }

    public class GameView extends SurfaceView {
        Bitmap bmp,pause;
        Bitmap background;
        Bitmap run1;
        Bitmap run2;
        Bitmap run3;
        Bitmap exit;

        private SurfaceHolder holder;
        private int x = 0,y=0,z=0,delay=0,getx,gety;
        int show=0,sx,sy;
        int cspeed=0,kspeed=0,gameover=0;
        int score=0,health=100,reset=0;
        int pausecount=0,volume,power=0,powerrun=0,shieldrun=0;

        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        public GameView(Context context) 
          {
              super(context);

              gameLoopThread = new gameloop(this);
              holder = getHolder();

                 holder.addCallback(new SurfaceHolder.Callback() {
                @SuppressWarnings("deprecation")
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) 
                {
                    gameLoopThread.setRunning(false);
                    gameLoopThread.getThreadGroup().interrupt();
                }

                @SuppressLint("WrongCall")
                @Override
                public void surfaceCreated(SurfaceHolder holder) 
                {
                      gameLoopThread.setRunning(true);
                      gameLoopThread.start();

                }
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format,int width, int height) 
                        {

                        }
                });



                Display display = getWindowManager().getDefaultDisplay();

                sx = display.getWidth();
                sy = display.getHeight();;
                background = BitmapFactory.decodeResource(getResources(), R.drawable.back);
                run1=BitmapFactory.decodeResource(getResources(), R.drawable.run1);
                run2=BitmapFactory.decodeResource(getResources(), R.drawable.run2);
                run3=BitmapFactory.decodeResource(getResources(), R.drawable.run3);
                exit=BitmapFactory.decodeResource(getResources(), R.drawable.exit);
                pause=BitmapFactory.decodeResource(getResources(), R.drawable.pause);

                exit=Bitmap.createScaledBitmap(exit, 25,25, true);
                pause=Bitmap.createScaledBitmap(pause, 25,25, true);
                run1=Bitmap.createScaledBitmap(run1, sx/9,sy/7, true);
                run2=Bitmap.createScaledBitmap(run2, sx/9,sy/7, true);
                run3=Bitmap.createScaledBitmap(run3, sx/9,sy/7, true);
                background=Bitmap.createScaledBitmap(background, 2*sx,sy, true);

    }


     // on touch method

          @Override
            public boolean onTouchEvent(MotionEvent event) {

                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {
                    show=1;

                    getx=(int) event.getX();
                    gety=(int) event.getY();

                // restart game
                if(getx>91&&gety<25)
                {
                    if(health<=0)
                    {
                        gameLoopThread.setPause(0);
                        health=100;
                        score=0;

                    }
                }
                //pause game
                if((getx>(sx-25)&&gety<25&&pausecount==0))
                {

                    gameLoopThread.setPause(1);
                    pausecount=1;
                }
                else if(getx>(sx-25)&&gety<25&&pausecount==1)
                {
                    gameLoopThread.setPause(0);
                    pausecount=0;
                }
            }

            return true;
        }


        @SuppressLint("WrongCall")
        @Override
        protected void onDraw(Canvas canvas) 
        {
            //background moving
            z=z-10;
            if(z==-sx)
            {
                z=0;
                canvas.drawBitmap(background, z, 0, null);

            }
            else
            {
                canvas.drawBitmap(background, z, 0, null);  
            }

            //running player 

         x+=5;
         if(x==20)
         {
             x=5;
         }

          if(show==0)
          {
              if(x%2==0)
              {
                  canvas.drawBitmap(run3, sx/16, 15*sy/18, null);

              }
              else 
              {
                  canvas.drawBitmap(run1, sx/16, 15*sy/18, null);

              }
          }

          canvas.drawBitmap(exit, 0, 0, null);
          canvas.drawBitmap(pause, (sx-25), 0, null);
        }
    }

    //phone state
    public class TeleListener extends PhoneStateListener 
    {
        public void onCallStateChanged(int state,String incomingNumber)
        {
            if(state==TelephonyManager.CALL_STATE_RINGING)
                {
                    System.exit(0);  
                }
        } 

    }
}

and this is my gameloop.java

package com.example.shadowassassinstu3;

import com.example.shadowassassinstu3.Game.GameView;
import android.annotation.SuppressLint;
import android.graphics.Canvas;

public class gameloop extends Thread {

    private GameView view;
    static final long FPS = 10;
    private boolean running = false;
       boolean isPaused;

    public gameloop(GameView view) {
          this.view = view;
    }

    public void setRunning(boolean run) {

          running = run;
    }

    public void setPause(int i)
    {
        synchronized (view.getHolder()) 
        {


            if(i==0)
            {
                isPaused=false;
            }
            if(i==1)
            {
                isPaused = true;
            }
         }
    }

    public void run() {
           long ticksPS = 100;
           long startTime = 0;
           long sleepTime;
              while (running) {
                  //pause and resume

                if (isPaused) 
                {
                      try 
                      {
                          this.sleep(50);
                      } 
                      catch (InterruptedException e) 
                      {
                        e.printStackTrace();
                      }
                }
                else
                {
                     Canvas c = null;
                     startTime = System.currentTimeMillis();
                     try {

                            c = view.getHolder().lockCanvas();

                            synchronized (view.getHolder()) 
                            {
                                view.onDraw(c);
                            }

                          } 
                     finally 
                     {
                         if (c != null) 
                            {
                                view.getHolder().unlockCanvasAndPost(c);
                            }
                     }
                   }
                     sleepTime = ticksPS-(System.currentTimeMillis() - startTime); 

                     try {

                            if (sleepTime > 0)
                               sleep(sleepTime);
                            else
                               sleep(10);
                        } 
                catch (Exception e) {}

              }

        }
}

A lot of the code I've gotten through watching tutorials and I really can't figure out what's wrong, if anyone could help I'd really appreciate it!

I think this is what you guys asked for:

05-11 10:23:51.966: D/AndroidRuntime(1894): Shutting down VM 05-11 10:23:51.966: D/AndroidRuntime(1894): --------- beginning of crash 05-11 10:23:51.966: E/AndroidRuntime(1894): FATAL EXCEPTION: main 05-11 10:23:51.966: E/AndroidRuntime(1894): Process: com.example.shadowassassinstu3, PID: 1894 05-11 10:23:51.966: E/AndroidRuntime(1894): java.lang.IllegalStateException: Could not execute method of the activity 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View$1.onClick(View.java:4007) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View.performClick(View.java:4756) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View$PerformClick.run(View.java:19749) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.os.Handler.handleCallback(Handler.java:739) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.os.Handler.dispatchMessage(Handler.java:95) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.os.Looper.loop(Looper.java:135) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.ActivityThread.main(ActivityThread.java:5221) 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Native Method) 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Method.java:372) 05-11 10:23:51.966: E/AndroidRuntime(1894): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 05-11 10:23:51.966: E/AndroidRuntime(1894): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 05-11 10:23:51.966: E/AndroidRuntime(1894): Caused by: java.lang.reflect.InvocationTargetException 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Native Method) 05-11 10:23:51.966: E/AndroidRuntime(1894): at java.lang.reflect.Method.invoke(Method.java:372) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.view.View$1.onClick(View.java:4002) 05-11 10:23:51.966: E/AndroidRuntime(1894): ... 10 more 05-11 10:23:51.966: E/AndroidRuntime(1894): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.shadowassassinstu3/com.example.shadowassassinstu3.Game}; have you declared this activity in your AndroidManifest.xml? 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1761) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1485) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivityForResult(Activity.java:3736) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivityForResult(Activity.java:3697) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivity(Activity.java:4007) 05-11 10:23:51.966: E/AndroidRuntime(1894): at android.app.Activity.startActivity(Activity.java:3975) 05-11 10:23:51.966: E/AndroidRuntime(1894): at com.example.shadowassassinstu3.MainActivity.play(MainActivity.java:25) 05-11 10:23:51.966: E/AndroidRuntime(1894): ... 13 more 05-11 10:23:55.283: I/Process(1894): Sending signal. PID: 1894 SIG: 9

Is it ok to declare an object to be equal to itself in Javascript for the sake of refactoring a large project

I'm working on a very large javascript web app that doesn't really have a method to it. There seems to be an attempt to declare everything as part of a master object called "app". The original program existed as a single app.js file over 300k lines of code where the following was declared:

var app = {};

Beyond that everything in the app is written as such.

app.somefunction = function(args, callback) { 
    some code
};

This apparently allowed for the author to handily use Eclipse IDE "outline", which I confess I have started to enjoy having, never previously having been an IDE user I'm becoming a convert to the niceties they provide.

My question is, as I work on a phased refactor of this huge codebase, potentially trying to merge it into some sort of more established framework perhaps using something like require.js, is it OK to split the "app.js" up into smaller files and for the sake of sanity to be able to use the IDE outline declare app to be equal to itself in each one.

app = app;

I've tested this, it works from what I can tell and it allows the IDE to add all the subsequent functions to the outline window while making the project a little more manageable while I learn what it is actually doing. Are there any drawbacks to doing this? I imagine some async loading issues might occur; this could possibly add to client side overhead; or is this perfectly acceptable?

I realize that this is sort of a code philosophy question, but the simple Q&A would be, what effect would app=app; have?

Upgrade to JUnit 4 in Eclipse

I tried switching to JUnit 4 in Eclipse using the instructions in this answer: http://ift.tt/1IwEE3T.

It basically says to remove any JUnit 3 library in the build path (I removed a JUnit 3.7 jar) and add the JUnit 4 library, which I did.

However, when I run the following code in my test class:

import junit.runner.Version;
System.out.println("JUnit version is: " + Version.id());

I get:

JUnit version is: 3.8.1

I don't have any other JUnit libraries in my buildpath.

If it's relevant, the exact Eclipse version is "Indigo Service Release 1, Build id: 20110916-0149".

How can I fix this and run JUnit 4?

Android Application run in a real Mobile but not in Eclipse

My application run in my real mobile phone but not in Eclipse+Android Studio Enviroment. I check class and Android Manifest and are same. Whats wrong ?

My codes are:

1) MainActivity.java

package com.example.homework2;

import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends ActionBarActivity 
{
    EditText firstnameText;
    Button goButton, aboutButton;

    @Override
    protected void onCreate( Bundle savedInstanceState ) 
    {
        super.onCreate( savedInstanceState );       
        setContentView( R.layout.activity_main );       

        //firstnameText = ( EditText ) findViewById( R.id.editText1 );
        //String name = firstnameText.getText().toString();
/*      
        goButton =  ( Button ) findViewById( R.id.letsgo_button );
        goButton.setOnClickListener( new OnClickListener() 
        {
            @Override
            public void onClick( View v ) 
            {
                // TODO Auto-generated method stub
            }
        }); 
*/      
        aboutButton = ( Button ) findViewById( R.id.about_button );
        aboutButton.setOnClickListener( new OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                Intent about = new Intent( MainActivity.this, About.class );
                startActivity( about );
            }           
        });
    }
}

2) activity_main.xml

<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
    xmlns:tools="http://ift.tt/LrGmb4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.homework2.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:scaleType="centerCrop"
        android:src="@drawable/wallpaper1" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:paddingTop="16px"
        android:paddingBottom="16px"
        android:background="#2B3856"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="Enter your Name: "
        android:textColor="#E8E8E8"
        android:textStyle="bold" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/about_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:background="#101010"
        android:text="About"
        android:textColor="#F8F8F8"
        android:textStyle="bold" />

    <Button
        android:id="@+id/letsgo_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/about_button"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_marginBottom="16dp"
        android:background="#3B0B0B"
        android:text="Let&apos;s GO !!!"
        android:textColor="#E8E8E8"
        android:textStyle="bold" />

</RelativeLayout>

3) About.java

package com.example.homework2;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;

public class About extends ActionBarActivity 
{   
    @Override
    protected void onCreate( Bundle savedInstanceState ) 
    {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.about );
    }
}

4) about.xml

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://ift.tt/nIICcg"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="463dp"
            android:layout_x="1dp"
            android:layout_y="-8dp"
            android:scaleType="centerCrop"
            android:src="@drawable/wallpaper2" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="match_parent"
            android:layout_height="209dp"
            android:layout_x="-1dp"
            android:layout_y="101dp"
            android:textColor="#FFFFFF"
            android:textStyle="bold|italic"   
            android:ems="10"
            android:gravity="center"
            android:text="Πριν δυο χρόνια ο Σερ Άλεξ Φέργκιουσον σταμάτησε την προπονητική. Η συγκεκριμένη εφαρμογή βάζει ένα κουίζ στους οπαδούς της ομάδας, για τα πεπραγμένα του Σκωτσέζου κόουτς στον πάγκο της Μάντσεστερ Γιουνάιτεντ." >

            <requestFocus />
        </EditText>
    </RelativeLayout>

5) AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://ift.tt/nIICcg"
    package="com.example.homework2"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".About"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.homework2.ABOUT" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Java - JSON and Parent Shells

I'm kind of new to Java and programming some first programs using SWT GUI with Eclipse.

I got a few things I don't understand and will appreciate some explanations, preferably expanded to me the easiest way possible.

Can someone please tell me what is the best way for me to save a String ArrayList of properties after the program is shut down?

I was told to save it to a text file, and use JSON to read and write, but I could not understand how to do it. I need to be able to get this information from the user and save it for future use.

Is JSON the best way for my needs?

For example, I will need to save these information arrays:

folder: D:\Videos
buttonsPerLine: 7
labelLength: 75
menuButtonLength: 60
spaceFromLeft: 20
spaceFromTop: 20
Shows:
    brooklyn nine nine
    community
    the big bang theory
    the blacklist

and these:

linkArray1:
    www.youtube.com/1
    www.youtube.com/2
    www.youtube.com/3
linkArray2:
    www.youtube.com/1
    www.youtube.com/2
    www.youtube.com/3
linkArray3:
    www.youtube.com/1
    www.youtube.com/1

Thank you very much!

Can't launch the java program

I found this code on the site oracle but I can't launch with Eclipse it's saying me "Unable to launch". I have installed JDK 8 but it doesn't work...

Anybody have a solution please ? :p

public class SwingFX extends Application {

@Override
public void start (Stage stage) {
    final SwingNode swingNode = new SwingNode();

    createSwingContent(swingNode);

    StackPane pane = new StackPane();
    pane.getChildren().add(swingNode);

    stage.setTitle("Swing in JavaFX");
    stage.setScene(new Scene(pane, 250, 150));
    stage.show();
    }

private void createSwingContent(final SwingNode swingNode) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            swingNode.setContent(new JButton("Click me!"));
        }
    });
}
}

link to the website where I found the code : http://ift.tt/1Ffsv46