Return to Snippet

Revision: 61707
at January 9, 2013 04:03 by DSTG_Kwan


Initial Code
/////////////////////////////////// Klasa Kalkulator.java ///////

/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package hr.foi.dstg.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Kalkulator implements EntryPoint {
	
	public void onModuleLoad() {
		RootPanel rootPanel = RootPanel.get();
		
		Calc calc = new Calc();
		rootPanel.add(calc, 10, 10);
		calc.setSize("344px", "223px");

		
	}
}


/////////////////////////////////// Klasa Calc.java ///////

package hr.foi.dstg.client;

import java.util.Random;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.NumberLabel;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class Calc extends Composite {
	
	RadioButton rdbtnOdabirFormata1 = new RadioButton("new name", "Grad - 3 brojke - 2 slova");
	RadioButton rdbtnOdabirFormata2 = new RadioButton("new name", "Grad - 4 brojke - 2 slova");
	RadioButton rdbtnOdabirFormata3 = new RadioButton("new name", "Grad - 3 brojke - 1 slovo");
	RadioButton rdbtnOdabirFormata4 = new RadioButton("new name", "Grad - 4 brojke - 1 slovo");
	
	NumberLabel<Integer> lblIspisBrojaPermutacija = new NumberLabel<Integer>();
	Label lblIspisregistracije = new Label("");
	
	public static String generateString(Random rng, String characters, int length)
	{
	    char[] text = new char[length];
	    for (int i = 0; i < length; i++)
	    {
	        text[i] = characters.charAt(rng.nextInt(characters.length()));
	    }
	    return new String(text);
	}

	public Calc() {
		
		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.setBorderWidth(0);
		initWidget(verticalPanel);
		
		Label lblNaslov = new Label(" Odaberite format registracijske oznake: ");
		lblNaslov.setStyleName("gwt-DialogBox");
		verticalPanel.add(lblNaslov);
		lblNaslov.setWidth("449px");
		
		FlexTable flexTable = new FlexTable();
		flexTable.setBorderWidth(0);
		verticalPanel.add(flexTable);
		
		rdbtnOdabirFormata1.setValue(true);
		flexTable.setWidget(0, 0, rdbtnOdabirFormata1);		
		flexTable.setWidget(1, 0, rdbtnOdabirFormata2);		
		flexTable.setWidget(2, 0, rdbtnOdabirFormata3);		
		flexTable.setWidget(3, 0, rdbtnOdabirFormata4);
		
		Button btnIzracunaj = new Button("Izra\u010Dunaj!");		
		verticalPanel.add(btnIzracunaj);
		
		FlexTable flexTable_1 = new FlexTable();
		flexTable_1.setBorderWidth(0);
		verticalPanel.add(flexTable_1);
		flexTable_1.setWidth("338px");
		
		Label lblBrojPermutacija = new Label("Broj permutacija:");
		flexTable_1.setWidget(0, 0, lblBrojPermutacija);		
		
		flexTable_1.setWidget(0, 1, lblIspisBrojaPermutacija);
		
		Label lblSluajnoGeneriranaRegistracija = new Label("Slu\u010Dajno generirana registracija: ");		
		flexTable_1.setWidget(1, 0, lblSluajnoGeneriranaRegistracija);
		
		
		flexTable_1.setWidget(1, 1, lblIspisregistracije);
		
		Label lblBrojSvihMoguih = new Label("Broj svih mogu\u0107ih registracija: ");
		flexTable_1.setWidget(2, 0, lblBrojSvihMoguih);
		
		NumberLabel<Integer> lblIspisUkupanBroj = new NumberLabel<Integer>();
		flexTable_1.setWidget(2, 1, lblIspisUkupanBroj);
		flexTable_1.getCellFormatter().setHorizontalAlignment(2, 1, HasHorizontalAlignment.ALIGN_LEFT);
		flexTable_1.getCellFormatter().setHorizontalAlignment(0, 1, HasHorizontalAlignment.ALIGN_LEFT);
		flexTable_1.getCellFormatter().setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT);
		flexTable_1.getCellFormatter().setHorizontalAlignment(1, 1, HasHorizontalAlignment.ALIGN_LEFT);
		lblIspisUkupanBroj.setValue(5009400);
		
		btnIzracunaj.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				
				int pom = 0;
				Random generator = new Random(System.currentTimeMillis());
				int broj = 0;
				String slova = new String();
				
				if (rdbtnOdabirFormata1.getValue()) {
					pom = 9*10*10*22*22; 
					lblIspisBrojaPermutacija.setValue(pom);
					broj = generator.nextInt(900) + 100;
					slova = generateString(generator, "ABCDEFGHIJKLMNOPRSTUVZ", 2);
					lblIspisregistracije.setText("ZG - " + String.valueOf(broj) + " - " + slova);
				} 
				
				else if (rdbtnOdabirFormata2.getValue()) {
					pom = 9*10*10*10*22*22; 
					lblIspisBrojaPermutacija.setValue(pom);
					broj = generator.nextInt(900) * generator.nextInt(10) + 1100;
					slova = generateString(generator, "ABCDEFGHIJKLMNOPRSTUVZ", 2);
					lblIspisregistracije.setText("ZG - " + String.valueOf(broj) + " - " + slova);
				} 
				
				else if (rdbtnOdabirFormata3.getValue()) {
					pom = 9*10*10*22; 
					lblIspisBrojaPermutacija.setValue(pom);
					broj = generator.nextInt(900) + 100;
					slova = generateString(generator, "ABCDEFGHIJKLMNOPRSTUVZ", 1);
					lblIspisregistracije.setText("ZG - " + String.valueOf(broj) + " - " + slova);
				} 
				
				else if (rdbtnOdabirFormata4.getValue()) {
					pom = 9*10*10*10*22; 
					lblIspisBrojaPermutacija.setValue(pom);
					broj = generator.nextInt(900) * generator.nextInt(10) + 1100;
					slova = generateString(generator, "ABCDEFGHIJKLMNOPRSTUVZ", 1);
					lblIspisregistracije.setText("ZG - " + String.valueOf(broj) + " - " + slova);
				} ;				
				
			}
		});		
	}

}

Initial URL
http://free.hostingjava.it/-ikovic/Kalkulator.html

Initial Description
Web aplikacija generira slučajne registracijske oznake za grad Zagreb u odabranom formatu. Ispisuje se i broj mogućih različitih registracijskih tablica za taj format.

Initial Title
DSTG_Projekt_WebKalkulator_

Initial Tags
window, java, google

Initial Language
Java