Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Importing External Code8:18 with Craig Dennis
Not all code is developed in the same environment. Let's learn how to bring in outside project into our shiny new IDE.
- In the course downloads zip file, use the Karaoke Project
Okay, so remember that quiz question from a previous 0:01 course where I asked about a dunk tank? 0:03 Well some of you didn't know what that was and I apologize. 0:05 It's a carnival game where someone sits over a pool of water on a shelf. 0:08 The carnival-goer buys the balls and they throw it at the target. 0:12 If they hit the target, the person falls into the water. 0:16 Now I'm sure you're imagining putting me on that dunk tank and 0:20 throwing the ball and having me dunked into freezing cold water, 0:23 because I didn't show you these tools earlier. 0:27 But, before you get too upset at me, let's take a second to appreciate that you were 0:29 able to code in Java without these tools. 0:33 You didn't need these training wheels on your editor. 0:36 Let's be honest, most of the warnings wouldn't have made any sense to you at 0:38 all until you got started. 0:42 Now, that being said though, you can totally dunk me. 0:44 I'm sure I deserve it. 0:47 I did make you type a lot more than you would have to in a real world situation. 0:48 So let's just say at this point, I definitely owe you three dunk tank balls. 0:52 Alright, so let's import that very long project that you painstakingly 0:56 completed without code assistance in the previous course, the karaoke machine. 1:01 I want to show you how to bring outside code into this new world order. 1:06 Okay, so I've included a zip file of 1:10 the source code from the final project in the teacher's notes. 1:13 If you want to get your own version, just go to your workspace and choose File > 1:16 Download Workspace, and that will send down a ZIP file that you can then open up. 1:20 So what I've done is I've downloaded that ZIP file, I've unzipped it, and 1:24 now I have that source code in a folder. 1:26 Here it is. 1:28 Now, since we're just hand coding like this and there isn't 1:30 any additional project information, let's first just create a new project. 1:32 Okay. So, the way that you do that is, 1:37 in IntelliJ you choose File > New > Project. 1:38 Okay. And this is very much like the Create New 1:43 Project that we saw before, and we wanna just leave it, the Project SDK the same. 1:45 We don't wanna do a template, right? 1:50 Because we've already got our code that we need, so we're just gonna go ahead and 1:51 leave that unchecked. 1:54 And we're gonna call this Karaoke. 1:56 Okay. So, it's possible to have multiple windows 2:01 open with different editors and that's what this prompt is asking. 2:04 It's asking if we want to replace this window or if we wanna make a new one and 2:07 have them side by side. 2:11 For our case, we're just gonna use this one. 2:12 So what it's gonna do is it's gonna get rid of the systemizer window that we have 2:14 and we're gonna put up the karaoke window. 2:17 Cool. 2:20 So, this is an empty project. 2:21 By default, the project is set up in a directory based format. 2:24 When creating a project, a default module is added for us, and 2:27 that's what this is here, this karaoke. 2:31 Now a module in IntellaJ is defined as a discreet unit of functionality. 2:33 Modules contain everything that is required for there specific task, and 2:38 they come with an idea folder. 2:43 This .Ideafolder. 2:44 It contains files about how you like your editor setup. 2:46 They also have an IML file here, and 2:49 it keeps project specific settings in one place. 2:51 The .iml suffix stands for idea module, and the fact that all the settings are in 2:54 a file like this, it makes sure that teams can have the same setup. 2:59 So I could share this with somebody else and this setup would be the same. 3:02 There's a little icon on this folder here, on the karaoke folder here of this module. 3:06 And that's saying that its the content root folder.This source folder here 3:11 is short for source code. 3:16 And its blue you'll notice, and that blue represents that its the source root. 3:18 All other folders are sort of orange and yellow like that. 3:22 If you're working with other projects from idea users or even in clips projects, 3:26 you can actually import things really easy. 3:30 So, you could get these INL files from other people like I was saying. 3:32 But what I wanna show you here first is that when you don't 3:36 have an existing idea project to import, like we don't currently, right? 3:39 Our karaoke project is hand-coded. 3:42 You can imagine this as kind of converting or upgrading to an idea project. 3:44 These idea projects respond really well to dragging and dropping. 3:49 So, I'm gonna go ahead and I am gonna grab the karaoke folder and our packages. 3:53 And I'm gonna drag those and 3:59 I'm gonna just click right inside the source file here, okay? 4:01 And it's gonna go ahead and say move the specified references and 4:05 that's fine, right? 4:07 Cuz that was just a zip folder that I had. 4:08 So I'm just gonna go ahead and move them. 4:09 And it says open the moved files in the editor. 4:11 Sure, why not? 4:13 Okay, and it says they don't belong to the project. 4:15 Okay I understand that. 4:18 I'm moving them. 4:19 And finally, I'm gonna bring this songs.text to the content root. 4:22 Right? So I'm gonna put it here. 4:27 When we end up running this later and 4:28 again it asks for the same things here again. 4:30 Before we were running right from the start of the directory and 4:32 this is relative. 4:36 We'll explore this a little bit in the future when we go to run this. 4:37 But I just wanted to point out that we don't want to drop it into source code 4:41 folder, we're gonna put it in the content root, and I'll show you why here in a bit. 4:44 Okay, so, I really wanna show you something that's gonna make you happy. 4:47 So, you can set up these different formatting rules. 4:52 So, here's our song.java file. 4:55 Now let's say that when we were coding, 4:57 we weren't the cleanest about things about things that we kind of did. 4:59 We move this over, got a warning up here about the file is indented with 5:03 two spaces instead of four, and that's kind of the default here, in this world. 5:09 And this is nice, right? 5:12 Cuz if you have different team members working on different things. 5:14 So, I'm just gonna screw up the spacing a little bit because, you know, 5:16 we were rushing to get through this before and the spacing wasn't quite done. 5:19 So, let's go ahead and come up here to code. 5:23 And if you look down here in this code, there's this Reformat code, 5:28 and that is Option+Command+L on a Mac. 5:35 So I'm gonna go ahead and I'm gonna click that. 5:39 Watch what it does. 5:40 Boom. Everything got set the proper way and 5:42 now it's also at four spaces. 5:44 But look, everything's back to where it was. 5:45 So, I am gonna do that again and I'm gonna do that again and 5:47 we'll just bring this guy. 5:51 It's pretty good at not letting me screw those up. 5:54 So, I'm going to Option+Command+L and everything is back to normal. 5:57 So it's Ctrl+Alt+L in Windows. 6:02 That's great. 6:03 But what if I knew that I remembered doing that one time, but 6:04 I can't remember where it's at? 6:08 So there's a great feature in IntelliJ whic will be your friend, 6:09 especially when you're learning this. 6:12 If you press Cmd+Shift+A, or Shift+Ctrl+A on Windows, and you choose. 6:14 I know it wasn't Reformat. 6:22 There it is, Reformat Code. 6:25 And then, boom. 6:26 And it will do it. 6:27 And I have to break anything, let's break something real quick. 6:29 So, Control+Shift+A, Reformat Code, boom, that fixed it. 6:34 Great, right? 6:38 So, we just did this file, but let's go ahead and open up another file over here. 6:39 Let's open up Songbook. 6:44 So we can go ahead and reformat the entire package. 6:46 So, if I come up here to the source folder and I say Reformat Code, 6:50 we're gonna include subdirectories and we're gonna leave Optimize Imports, right? 6:57 So that's gonna remove any imports that we didn't use, cuz that can happen, and 7:01 we're also gonna choose Rearrange Entry. 7:05 So, I'm gonna click Run. 7:07 So, now all the files, and 7:10 we can open them up by clicking on them over here, are done. 7:12 You also notice there's some of these pluses. 7:16 This is here to kind of help you read your code quicker, but I'm gonna open this up. 7:18 One more thing I wanted to show you really quick is, on the imports over here, 7:23 remember we put the dot star, and I told you it was a little bit lazy? 7:26 Well let's go ahead and let's put our cursor here and let's see what happens. 7:30 Look, there's a little thing that popped up over here. 7:32 This light bulb is called an intention action. 7:34 What happens with these intention actions are they're suggestions 7:37 of what could happen. 7:40 So, let's go ahead and open this up and it says, replace with single class imports. 7:42 That's great, that's exactly what we wanted. 7:46 We just didn't want to have to type all those out, so let's do that. 7:47 And what it did was it took an alphabetized order of everything that we 7:50 used in here that was under that star. 7:54 Pretty powerful tool, isn't it? 7:56 Okay, we learned a ton there. 7:58 And as I'm sure, 8:00 you saw when you opened that menu up, there are a lot more actions in there. 8:01 We definitely aren't gonna hit all of those, but in a bit, I'll show you how 8:04 awesome the documentation is for the IntelliJ Idea Project. 8:08 You can dive in at your own pace. 8:12 So let's go ahead and take a quick break and I'll swing back and 8:14 show you how to run our application. 8:16
You need to sign up for Treehouse in order to download course files.Sign up